亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從擦除轉換泛型類型

如何從擦除轉換泛型類型

catspeake 2021-10-28 17:07:04
我有一個父類,Parent有兩個子類,A和B. 我有一個接口,Function<Type1 extends Parent,Type2 extends Parent>允許程序員編寫一個特定的函數,Type2 of(Type1 t)將 Type1 轉換為 Type2。該接口封裝在一個類中,該類Wrapper<Type1 extends Parent,Type2 extends Parent>包含有用的信息,例如Class<Type1> type1Class等。我的問題出現了,當我嘗試實施add該方法的Wrapper類Wrapper<Type1,Type2> add(Wrapper<Type1,Type2> additionalWrapper)。我正在嘗試將兩個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 回答

?
動漫人物

TA貢獻1815條經驗 獲得超10個贊

需要在單個覆蓋函數中使用 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;

}


查看完整回答
反對 回復 2021-10-28
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

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


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()); }

}

因為add是在Parent中定義的,所以只有一種實現就足夠了。在當前場景中,因為addin Class的簽名A與簽名 in 不匹配Parent,因此該方法根本沒有被覆蓋


如果您添加@Override添加方法,您就會知道。編譯器會拋出錯誤。


希望這可以幫助。


查看完整回答
反對 回復 2021-10-28
  • 2 回答
  • 0 關注
  • 161 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號