日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2024/7/5 java 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java implements t_Java泛型——為什么“擴展T”允許而不是“實現T”? 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

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

在類“實現(xiàn)”或“擴(kuò)展”之間的通用約束語言中沒有語義差異。約束的可能性是“擴(kuò)展”和“超級”——也就是說,這個類是與其他一個(擴(kuò)展)的賦值操作的,或者是這個類可以從那個(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).

要聲明一個有界類型參數(shù),請列出類型參數(shù)的名稱,然后是擴(kuò)展關(guān)鍵字,然后是它的上界[…]。注意,在這個上下文中,擴(kuò)展被用於一般意義上,表示擴(kuò)展(如在類中)或?qū)崿F(xiàn)(如在接口中)。

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),只有類型是相關(guān)的,而不是實現(xiàn)。在你的例子

public class A{}

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

B也可以是一個接口?!皵U(kuò)展”用於定義子接口和子類。

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.

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

#4

7

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

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

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:

可能是基類型是一個泛型參數(shù),所以實際類型可能是類的接口??紤]:

class MyGen {

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

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

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

這是一種任意使用的術(shù)語。它本來可以是任何一種方式。也許語言設(shè)計者認(rèn)為“擴(kuò)展”是最基本的術(shù)語,而“實現(xiàn)”是接口的特殊情況。

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.

但我認(rèn)為實施會更有意義。我認(rèn)為,更多的溝通,參數(shù)類型不一定要在繼承關(guān)系中,它們可以在任何類型的子類型關(guān)系中。

The Java Glossary expresses a similar view.

Java術(shù)語表表達(dá)了類似的觀點。

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

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

總結(jié)

以上是生活随笔為你收集整理的java implements t_Java泛型——為什么“擴展T”允許而不是“實現T”?的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。