第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何從擦除轉(zhuǎn)換泛型類型

如何從擦除轉(zhuǎn)換泛型類型

catspeake 2021-10-28 17:07:04
我有一個(gè)父類,Parent有兩個(gè)子類,A和B. 我有一個(gè)接口,F(xiàn)unction<Type1 extends Parent,Type2 extends Parent>允許程序員編寫一個(gè)特定的函數(shù),Type2 of(Type1 t)將 Type1 轉(zhuǎn)換為 Type2。該接口封裝在一個(gè)類中,該類Wrapper<Type1 extends Parent,Type2 extends Parent>包含有用的信息,例如Class<Type1> type1Class等。我的問題出現(xiàn)了,當(dāng)我嘗試實(shí)施add該方法的Wrapper類Wrapper<Type1,Type2> add(Wrapper<Type1,Type2> additionalWrapper)。我正在嘗試將兩個(gè)Functions 相加,但由于擦除,我很難得到 aType2而不是 a 輸出Parent。如何使add方法輸出 aType2而不是 a Parent?public class Parent {    protected int value;    public void setValue(int x){ value = x; }    public int getValue(){ return value; }    public Parent(){}    public Parent(int x){setValue(x);}    public Parent add(Parent p){return null;}}public class A extends Parent{    public A(){ setValue(1); }    public A(int x){ setValue(x); }    public A(B b){ setValue( b.getValue()); }    public A add(A a){ return new A( getValue()+a.getValue()); }    public A add(B b){ return new A( getValue()*b.getValue()); }}public class B extends Parent{    public B(){ setValue(2); }    public B(int x){ setValue(x); }    public B(A a){ setValue(a.getValue()); }    public B add(B b){ return new B(getValue() + b.getValue()); }    public B add(A a){ return new B(getValue() * a.getValue()); }}public interface Function <Type1 extends Parent, Type2 extends Parent> {    public Type2 of(Type1 t);}public class Wrapper<Type1 extends Parent, Type2 extends Parent> {    protected Function<Type1,Type2> function;    protected Class<Type1> type1Class;    protected Class<Type2> type2Class;    public Wrapper(final Class<Type1> t1, final Class<Type2> t2, Function<Type1,Type2> x) {        type1Class = t1;        type2Class = t2;        function = x;    }    public Type2 of(Type1 t){        return function.of(t);    }  
查看完整描述

2 回答

?
動(dòng)漫人物

TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊

需要在單個(gè)覆蓋函數(shù)中使用 instanceof Parent add(Parent p)。


在 A 類和 B 類中,需要具有以下功能:


@Override

public Parent add(Parent p){

? ? if (p instanceof A){

? ? ? ? A a = (A) p;

? ? ? ? return add(a);

? ? }

? ? else if (p instanceof B){

? ? ? ? B b = (B) p;

? ? ? ? return add(b);

? ? }

? ? else return null;

}


查看完整回答
反對 回復(fù) 2021-10-28
?
HUWWW

TA貢獻(xiàn)1874條經(jīng)驗(yàn) 獲得超12個(gè)贊

基本上你不用兩種方法來添加 A 和 B,這違反了多態(tài)性或 SOLID 原則。您可以像下面這樣構(gòu)建您的課程 -


class A extends Parent{

public A(){ setValue(1); }

public A(int x){ setValue(x); }

public A(B b){ setValue( b.getValue()); }

@Override

public A add(Parent a){ return new A( getValue()+a.getValue()); }

}


class B extends Parent{

public B(){ setValue(2); }

public B(int x){ setValue(x); }

public B(A a){ setValue(a.getValue()); }

@Override

public B add(Parent b){ return new B(getValue() + b.getValue()); }

}

因?yàn)閍dd是在Parent中定義的,所以只有一種實(shí)現(xiàn)就足夠了。在當(dāng)前場景中,因?yàn)閍ddin Class的簽名A與簽名 in 不匹配Parent,因此該方法根本沒有被覆蓋


如果您添加@Override添加方法,您就會(huì)知道。編譯器會(huì)拋出錯(cuò)誤。


希望這可以幫助。


查看完整回答
反對 回復(fù) 2021-10-28
  • 2 回答
  • 0 關(guān)注
  • 160 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號