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

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值