在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/Java/ Java: extends 后有&是什么意思

Java: extends 后有&是什么意思

看待一段代碼,一個類的定義,其中<E extends Enum<?> & BaseEnum> 這里的這個&是什么意思?

public class EnumTypeHandler<E extends Enum<?> & BaseEnum> extends BaseTypeHandler<BaseEnum>
回答
編輯回答
別逞強(qiáng)

1樓正解
& 在java中是and的意思,在泛型的應(yīng)用場景,含義基本不變。
public class EnumTypeHandler<E extends Enum<?> & BaseEnum> extends BaseTypeHandler<BaseEnum>
中的E extends Enum<?> & BaseEnum可以理解為 E extends (Enum<?> & BaseEnum),結(jié)合extends的含義,及E 為 Enum<?> 和 BaseEnum 的子類

2018年6月29日 16:21
編輯回答
萌吟
package TypeVarMembers;
class C {
    public void mCPublic() {}
    protected void mCProtected() {}
    void mCPackage() {}
    private void mCPrivate() {}
}
interface I {
    void mI();
}
class CT extends C implements I {
    public void mI() {}
}
class Test {
    <T extends C & I> void test(T t) {
        t.mI(); // OK
        t.mCPublic(); // OK
        t.mCProtected(); // OK
        t.mCPackage(); // OK
        t.mCPrivate(); // Compile-time error
    }
}
The type variable T has the same members as the intersection type C & I, which in turn
has the same members as the empty class CT, defined in the same scope with equivalent
supertypes. The members of an interface are always public, and therefore always inherited`
(unless overridden). Hence mI is a member of CT and of T. Among the members of C, all
but mCPrivate are inherited by CT, and are therefore members of both CT and T.
If C had been declared in a different package than T, then the call to mCPackage would
give rise to a compile-time error, as that member would not be accessible at the point where
T is declared.

來自 The Java? Language Specification, Java SE 8 Edition $ 4.4 Type Variables P58~59

2017年7月13日 21:43