I wonder if there is a special reason in Java for using always "extends" rather than "implements" for defining bounds of typeparameters.

我想知道在Java中是否有一個特殊的原因,因為使用總是“擴展”而不是“實現”來定義類型參數的界限。

Example:

例子:

public interface C {}

public class A{}

is prohibited but

是被禁止的,但

public class A{}

is correct. What is the reason for that?

是正確的。原因是什么?

7 个解决方案

#1

292

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).

在類“實現”或“擴展”之間的通用約束語言中沒有語義差異。約束的可能性是“擴展”和“超級”——也就是說,這個類是與其他一個(擴展)的賦值操作的,或者是這個類可以從那個(super)中指定的。

#2

27

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 […]. Note that, in this context, extends is used in a general sense to mean either extends (as in classes) or implements (as in interfaces).

要聲明一個有界類型參數,請列出類型參數的名稱,然后是擴展關鍵字,然后是它的上界[…]。注意,在這個上下文中,擴展被用於一般意義上,表示擴展(如在類中)或實現(如在接口中)。

So there you have it, it's a bit confusing, and Oracle knows it.

這就有點讓人困惑了,Oracle知道。

#3

14

Probably because for both sides (B and C) only the type is relevant, not the implementation. In your example

可能因為對於雙方(B和C),只有類型是相關的,而不是實現。在你的例子

public class A{}

B can be an interface as well. "extends" is used to define sub-interfaces as well as sub-classes.

B也可以是一個接口。“擴展”用於定義子接口和子類。

interface IntfSub extends IntfSuper {}

class ClzSub extends ClzSuper {}

I usually think of 'Sub extends Super' as 'Sub is like Super, but with additional capabilities', and 'Clz implements Intf' as 'Clz is a realization of Intf'. In your example, this would match: B is like C, but with additional capabilities. The capabilities are relevant here, not the realization.

我通常認為“Sub - extends Super”是“Sub - is like Super,但有附加功能”,“Clz實現Intf”作為“Clz是Intf的實現”。在您的示例中,這將匹配:B類似於C,但具有額外的功能。這里的功能是相關的,而不是實現。

#4

7

Here is a more involved example of where extends is allowed and possibly what you want:

這里有一個更復雜的例子,其中擴展是允許的,也可能是你想要的:

public class A>

公共類A

>。

#5

6

It may be that the base type is a generic parameter, so the actual type may be an interface of a class. Consider:

可能是基類型是一個泛型參數,所以實際類型可能是類的接口。考慮:

class MyGen {

Also from client code perspective interfaces are almost indistinguishable from classes, whereas for subtype it is important.

同樣,從客戶端代碼的角度來看,接口與類幾乎是不可區分的,而對於子類型來說,它是重要的。

#6

2

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. 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術語表表達了類似的觀點。

#7

-2

Because interfaces are just classes, except they don't have attributes or implementations. The only use of the keyword "implements" is to allow a class to inherit multiple interfaces, but not multiple classes, and we can see it in the code. I don't know if they will specify this in the future, but this is not a must.

因為接口只是類,但它們沒有屬性或實現。關鍵字“實現”的唯一用途是允許類繼承多個接口,而不是多個類,我們可以在代碼中看到它。我不知道他們是否會在將來指定這個,但這不是必須的。

java implements t_Java泛型——為什么“擴展T”允許而不是“實現T”?相关推荐

  1. BAT批量重命名文件擴展名

    ::rem 重命名文件 ::.txt轉為.sql for /f "delims=" %%i in ('dir /b /a-d /s "*.txt"') do r ...

  2. 14. Java基础之泛型

    一. 泛型概念的提出(为什么需要泛型)? 首先,我们看下下面这段简短的代码: 1 public class GenericTest { 2 3 public static void main(Stri ...

  3. java模型给泛型_java 泛型详解-绝对是对泛型方法讲解最详细的,没有之一

    对java的泛型特性的了解仅限于表面的浅浅一层,直到在学习设计模式时发现有不了解的用法,才想起详细的记录一下. 1. 概述 泛型在java中有很重要的地位,在面向对象编程及各种设计模式中有非常广泛的应 ...

  4. Java:反射+泛型:获取类型参数的实例

    2019独角兽企业重金招聘Python工程师标准>>> 场景描述: 我需要开发四个页面,每个页面都是只涉及增删改查的基本逻辑. 最简单的写法: 创建四个接口A,B,C,D,每个接口中 ...

  5. Java集合和泛型练习及面试题——博客园:师妹开讲啦

    给定一段JAVA代码如下:要打印出list中存储的内容,以下语句正确的是( B  ) ArrayList list = new ArrayList( ) list.add("a") ...

  6. java定义list_我的Java Web之路59 - Java中的泛型

    本系列文章旨在记录和总结自己在Java Web开发之路上的知识点.经验.问题和思考,希望能帮助更多(Java)码农和想成为(Java)码农的人. 目录 介绍 再谈Java中的类型 为什么需要泛型? J ...

  7. [转载] 详解Java中的泛型

    参考链接: Java中具有泛型的有界类型 1.什么是泛型 泛型,即"参数化类型".一提到参数,最熟悉的就是定义方法时有形参,然后调用此方法时传递实参.那么参数化类型怎么理解呢?顾名 ...

  8. 擴展 CommandField 類別 - 刪除提示訊息

    GridView 中 CommandField 的刪除鈕預設是沒有刪除提示訊息,一般的作法是在 GridView 的 RowDataBound 事件中找到 CommandField 中的按鈕來設定它的 ...

  9. java如何用反射把具体方法抽象_如何在Java 中使用泛型或反射机制对DAO进行抽象...

    如何在Java 中使用泛型或反射机制对DAO进行抽象 发布时间:2020-11-26 16:07:42 来源:亿速云 阅读:80 作者:Leah 本篇文章为大家展示了如何在Java 中使用泛型或反射机 ...

最新文章

  1. 零基础该如何学习Web前端知识?
  2. 管理者的智慧:靠制度管人,不靠人管人
  3. 是否可以在另一个CSS文件中包含一个?
  4. 计算机图形什么叫参数连续性,计算机图形学--参数三次插值样条曲线.ppt
  5. 200723学习日报
  6. Jerry Wang的CRM中间件培训 Middleware
  7. python排序sorted_sorted排序的两个方法 - Python
  8. 如何设置浏览器禁止使用UC浏览器
  9. hdu 2110 基础母函数
  10. 静态成员函数与静态成员变量
  11. wps单文件版_局域网共享精灵,轻松实现文件共享amp;打印机共享
  12. java win10_java下载64位win10-javawin10 64位下载8.0.1210.13官方版-西西软件下载
  13. 我们通常所说的利率是指_我们通常所说的利率是指()。 A.市场利率B.名义利率C.实际利率D.固定利率...
  14. 汇总 | 嵌入式软硬件领域各种“黑科技”
  15. 【规划】Motion Planning Approaches 路径规划方法汇总
  16. R语言——多元数据直观表示
  17. cv2 给图片加框框
  18. Spring Data ElasticSearch增删改查
  19. 面试官:MySQL索引为什么要用B+树实现?
  20. HDOJ--1052--Tian Ji -- The Horse Racing

热门文章

  1. java启动密码,java - 用户验证密码后如何启动java applet程序? - SO中文参考 - www.soinside.com...
  2. [BZOJ 4025] 二分图
  3. 第二节:Css重写样式
  4. Win7_刻录DVD
  5. FPGA开发全攻略——概念篇
  6. 关于html标签元素的data-*属性
  7. php调用webservice报错Class 'SoapClient' not found
  8. ubuntu 安装 LAMP
  9. Asp.net 中 Eval 调用后台函数的写法
  10. 物联网是互联网发展的必然趋势吗?