天天看點

Java泛型-為什麼允許“擴充T”但不允許“實作T”?

本文翻譯自: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 ClassType

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

問題是我們沒有一個合适的詞來涵蓋

extends

implements

,我們絕對不想引入一個。

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

限制可能性是“擴充”和“超級”-也就是說,該類是可配置設定給其他類的對象(擴充),還是該類可從該類配置設定(超級)。