本文翻译自:Java generics - why is “extends T” allowed but not “implements T”?

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<B implements C>{}

is prohibited but 被禁止但是

public class A<B extends C>{}

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


#1楼

参考:https://stackoom.com/question/4613/Java泛型-为什么允许-扩展T-但不允许-实现T


#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 similar view . Java术语表表达了类似的观点 。


#4楼

The answer is in here : 答案在这里 :

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}

( JLS 12 > 4.4. Type Variables > TypeBound ) ( JLS 12> 4.4。类型变量> TypeBound

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

( JLS 12 > 4.3. Reference Types and Values > ClassOrInterfaceType ) ( JLS 12> 4.3。引用类型和值> ClassOrInterfaceType

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). 约束可能性是“扩展”和“超级”-也就是说,该类是可分配给其他类的对象(扩展),还是该类可从该类分配(超级)。

Java泛型-为什么允许“扩展T”但不允许“实现T”?相关推荐

  1. Java学习笔记(二一)——Java 泛型

    [前面的话] 最近脸好干,掉皮,需要买点化妆品了. Java泛型好好学习一下. [定义] 一.泛型的定义主要有以下两种: 在程序编码中一些包含类型参数的类型,也就是说泛型的参数只可以代表类,不能代表个 ...

  2. 用了这么多年的 Java 泛型,你对它到底有多了解?|原创

    作为一个 Java 程序员,日常编程早就离不开泛型.泛型自从 JDK1.5 引进之后,真的非常提高生产力.一个简单的泛型 T,寥寥几行代码, 就可以让我们在使用过程中动态替换成任何想要的类型,再也不用 ...

  3. J2EE Java泛型的好处

    Java泛型的好处  java 泛型是java SE 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型 ...

  4. Java泛型中extends T和super T的区别?

    <? extends T>和<? super T>是Java泛型中的"通配符(Wildcards)"和"边界(Bounds)"的概念. ...

  5. Java泛型中? 和 ? extends Object的异同分析

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 作者 | 刘一手 来源 | 公众号「锅外的大佬」 Jav ...

  6. Java泛型解析(02):通配符限定

    Java泛型解析(02):通配符限定      考虑一个这种场景.计算数组中的最大元素. [code01] public class ArrayUtil {public static <T> ...

  7. Json反序列化与Java泛型

    Java的JSON库有很多,本文分析google的Gson和alibaba的fastjson,在Java泛型场景反序列化的一些有意思的行为.考虑下面的json字符串: ["214748364 ...

  8. java泛型(二)、泛型的内部原理:类型擦除以及类型擦除带来的问题

    原 java泛型(二).泛型的内部原理:类型擦除以及类型擦除带来的问题 2012年08月29日 23:44:10 Kilnn 阅读数:56717 版权声明:本文为博主原创文章,未经博主允许不得转载. ...

  9. java 通配符 类_关于类:具有多个类的Java泛型通配符

    我想要一个类对象,但是我想要强制它所代表的任何类来扩展类A和实现接口B. 我能做到: Class extends ClassA> 或: Class extends InterfaceB> ...

最新文章

  1. 项目新的需求,网页的自适应交付/响应式交付 Responsive/Adaptive Delivery
  2. 手机上有android,android-在不同智能手机上的Videoview行为(具有...
  3. powerdesigner导出sql时自动生成注释
  4. Scala 内部类及外部类
  5. Everything是如何搜索的
  6. 谷歌官方推出 TensorFlow 中文视频:机器学习从零到一
  7. windows平台下subversion服务器端配置
  8. python第四章单元测试_Python 单元测试
  9. AGG第十八课 agg::trans_affine仿射变换
  10. 概率图模型笔记(一)开坑
  11. 一小时入门proteus使用教程
  12. M2Det: A Single-Shot Object Detector based on Multi-Level Feature Pyramid Network AAAI2019
  13. 使用C++对himawari8(9)卫星数据的读取
  14. MTK手机平台及相关
  15. 2019年第十届蓝桥杯真题解析 | 特殊数【Python】
  16. 跟着CELL学作图|1.火山图
  17. ajax header请求,如何在用户登陆之后给每个页面往后台的ajax请求header都带上
  18. Linux - 拨号上网
  19. 海思PQTool进行CCM调试经验
  20. Cesium淹没分析(干货)

热门文章

  1. android的UI开发工程师指引
  2. 面试---如何在List<Integer>中如何存放String类型的数据?
  3. 2020年总结以及21年规划
  4. 算法--------旋转数组
  5. 老式Android中碎片Fragment之间的跳转和数据传递
  6. React创建组件的三种方式及其区别
  7. Mysql数据库事务详解
  8. 嵌入式CGI开发之旅——CGI环境变量
  9. Object.defineProperty的理解
  10. Remove Nth Node From End of List - LeetCode