博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java泛型-为什么允许“扩展T”但不允许“实现T”?
阅读量:2289 次
发布时间:2019-05-09

本文共 3801 字,大约阅读时间需要 12 分钟。

本文翻译自:

I wonder if there is a special reason in Java for using always " extends " rather than " implements " for defining bounds of typeparameters. 我想知道在Java中是否有一个特殊的原因总是使用“ extends ”而不是“ implements ”来定义类型参数的界限。

Example: 例:

public interface C {}public class A{}

is prohibited but 被禁止但是

public class A{}

is correct. 是正确的。 What is the reason for that? 是什么原因呢?


#1楼

参考:


#2楼

这是一个允许在何处进行扩展以及可能需要的扩展的示例:

public class A<T1 extends Comparable<T1>>


#3楼

It's sort of arbitrary which of the terms to use. 使用哪种术语是任意的。 It could have been either way. 可能是任何一种方式。 Perhaps the language designers thought of "extends" as the most fundamental term, and "implements" as the special case for interfaces. 也许语言设计师认为“扩展”是最基本的术语,而“实现”则是接口的特殊情况。

But I think implements would make slightly more sense. 但是我认为implements会更有意义。 I think that communicates more that the parameter types don't have to be in an inheritance relationship, they can be in any kind of subtype relationship. 我认为传达更多信息的是,参数类型不必处于继承关系中,它们可以处于任何类型的子类型关系中。

The Java Glossary expresses a . Java术语表表达了 。


#4楼

The answer is in : 答案在 :

To declare a bounded type parameter, list the type parameter's name, followed by the extends keyword, followed by its upper bound […]. 要声明一个有界的类型参数,请列出该类型参数的名称,然后列出extends关键字,然后列出其上限 […]。 Note that, in this context, extends is used in a general sense to mean either extends (as in classes) or implements (as in interfaces). 请注意,在这种情况下,extends通常用于表示extends (如在类中)或implements (如在接口中)。

So there you have it, it's a bit confusing, and Oracle knows it. 因此,有了它,这有点令人困惑,Oracle知道了。


#5楼

We are used to 我们习惯了

class ClassTypeA implements InterfaceTypeA {}class ClassTypeB extends ClassTypeA {}

and any slight deviation from these rules greatly confuses us. 与这些规则的任何细微差异都会使我们感到困惑。

The syntax of a type bound is defined as 类型绑定的语法定义为

TypeBound:    extends TypeVariable     extends ClassOrInterfaceType {AdditionalBound}

( ) ( )

If we were to change it, we would surely add the implements case 如果我们要改变它,我们肯定会添加implements的情况下

TypeBound:    extends TypeVariable     extends ClassType {AdditionalBound}    implements InterfaceType {AdditionalBound}

and end up with two identically processed clauses 最后有两个相同处理的子句

ClassOrInterfaceType:    ClassType     InterfaceType

( ) ( )

except we would also need to take care of implements , which would complicate things further. 除了我们还需要照顾implements ,这会使事情进一步复杂化。

I believe it's the main reason why extends ClassOrInterfaceType is used instead of extends ClassType and implements InterfaceType - to keep things simple within the complicated concept. 我认为,这是使用extends ClassOrInterfaceType而不是extends ClassTypeimplements InterfaceType主要原因-使事情在复杂的概念中保持简单。 The problem is we don't have the right word to cover both extends and implements and we definitely don't want to introduce one. 问题是我们没有一个合适的词来涵盖extendsimplements ,我们绝对不想引入一个。

<T is ClassTypeA>
<T is InterfaceTypeA>

Although extends brings some mess when it goes along with an interface, it's a broader term and it can be used to describe both cases. 虽然extends在与接口一起使用时会带来一些混乱,但它是一个广义术语,可以用来描述这两种情况。 Try to tune your mind to the concept of extending a type (not

extending a class
, not
implementing an interface
).
尝试将您的想法调整为扩展类型的概念(不
扩展类
,不
实现接口
)。
You restrict a type parameter by another type and it doesn't matter what that type actually is. 您可以将类型参数限制为另一种类型 ,实际上该类型是什么都没有关系。 It only matters that it's its upper bound and it's its supertype . 唯一重要的是它是它的上限和它的超类型


#6楼

There is no semantic difference in the generic constraint language between whether a class 'implements' or 'extends'. 在类“实现”还是“扩展”之间,通用约束语言没有语义差异。 The constraint possibilities are 'extends' and 'super' - that is, is this class to operate with assignable to that other one (extends), or is this class assignable from that one (super). 约束可能性是“扩展”和“超级”-也就是说,该类是可分配给其他类的对象(扩展),还是该类可从该类分配(超级)。

转载地址:http://wzjnb.baihongyu.com/

你可能感兴趣的文章
python 99乘法表
查看>>
一个可以拿来直接用的资产管理项目
查看>>
Centos 7 firewalld 基本操作
查看>>
passwd:只能指定一个用户的名称。
查看>>
nginx日志切割和日志清理
查看>>
RPC与分布式服务框架Dubbo
查看>>
为什么需要文件服务器?
查看>>
Redis怎么实现主从同步的
查看>>
Spring整理
查看>>
Spring Mvc整理
查看>>
Dubbo整理
查看>>
Redis整理
查看>>
JVM内存模型和类加载机制
查看>>
JDK1.0到12各版本新特性
查看>>
JAVA的一次编译,到处运行。可以在所有的平台上运行?
查看>>
HttpSessionListener监听器
查看>>
JSP
查看>>
Servlet九大内置对象
查看>>
JSTL
查看>>
el表达式
查看>>