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

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

如何使用泛型類檢查類的有效性

如何使用泛型類檢查類的有效性

哈士奇WWW 2021-12-01 16:54:55
我正在處理代碼,我必須將基類轉換為派生類,其中我有一組由基派生的泛型類型。例如,我有 Base 和 Derived1、Derived2 并將它們放入 Class[]{Derived1.class, Derived2.class} 并將這個數組傳遞給類的構造函數。在這個構造函數中,我必須創建這些派生類的實例,但我不知道該怎么做,因為我得到了 Class 和 Base 不兼容的信息。這是我的代碼示例public abstract class Base {public abstract Base create(String s);}public class Derived extends Base {java.lang.Integer value;private static Derived integer = new Derived();public static Derived getInstance(){    return integer;}public Base create(String s) {    value = java.lang.Integer.parseInt(s);    return this;}}public class Clazz {Class<? extends Base> type;ArrayList<Base> arrayList;public Class<? extends Base> getType() {    return type;}}public class AnotherClazz{ArrayList<Clazz> clazzArrayList;Class<? extends Base>[] types;AnotherClazz(Class<? extends Base>[] args){    clazzArrayList = new ArrayList<>();    types = args; // assuming I pass 2 elements in array    String[] strings = new String[]{"1","2"};    for (int i=0; i<args.length; ++i){        if (types[i] instanceof Base){            // here i want to check validity of class        }    }    for (int i=0; i<strings.length; ++i){        clazzArrayList.get(i).arrayList.add(((types[i]) Base).getInstance().create(strings[i]));     //here i want to create instance of object from type assigned to specific column    }}謝謝您的幫助。
查看完整描述

3 回答

?
一只斗牛犬

TA貢獻1784條經驗 獲得超2個贊

要檢查有效性,試試這個

if (types[i].getClass().isAssignableFrom(Base.class))


查看完整回答
反對 回復 2021-12-01
?
尚方寶劍之說

TA貢獻1788條經驗 獲得超4個贊

感謝您幫助檢查有效性(它有效?。┑胰匀粵]有得到這個 newInstance 創建,因為我必須從 .csv 文件中讀取數據,而我的派生類實際上是原始類型(如 int、float 等)的“包裝器” . 我應該使用方法 getInstance() 和 create(string s) 創建新對象,所以它看起來像這樣:


public static class Derived1 extends Base { //Integer wrapper


    Integer value;


    public Derived1(Integer value) {

        this.value = value;

    }


    private static Integer integer = new Integer();


    public static Integer getInstance(){

        return integer;

    }


    private Integer(){};


    public Base create(String s) {

        value = java.lang.Integer.parseInt(s);

        return this;

    }


}

而且我不知道如何使用 Class 轉換為適當的類型。


查看完整回答
反對 回復 2021-12-01
?
慕工程0101907

TA貢獻1887條經驗 獲得超5個贊

如果我正確閱讀了這個問題,那么您想創建一些派生類的實例,它們都具有相同的構造函數參數。如果是這種情況,那么您需要為每個派生類提供相同的構造函數(它不需要在基類中)并使用 Constructor.newInstance(parameters) 創建實例。此外,由于您要確保每個派生類擴展基類,因此您將需要使用 Class.isAssignableFrom(class)。例如,


import java.lang.reflect.Constructor;


public class SO52930530 {


    public abstract static class Base {


        public abstract <T> T getValue();

    }


    public static class Derived1 extends Base {


        String value;


        public Derived1(String value) {

            this.value = value;

        }


        public <T> T getValue() {

            return (T) value;

        }

    }


    public static class Derived2 extends Base {


        Integer value;


        public Derived2(String value) {

            this.value = new Integer(value);

        }


        public <T> T getValue() {

            return (T) value;

        }

    }


    public static void main(String... args) throws Exception {


        Class<? extends Base>[] extensions = new Class[]{Derived1.class, Derived2.class};

        String[] values = new String[]{"a", "1"};

        Base[] instances = new Base[values.length];


        for (int i = 0; i < instances.length; i++) {

            Class extension = extensions[i];

            if (Base.class.isAssignableFrom(extension)) {

                Constructor constructor = extension.getConstructor(String.class);

                instances[i] = (Base) constructor.newInstance(values[i]);

            }

        }


        for (int i = 0; i < instances.length; i++) {

            System.out.printf("%d %s %s\n", i, instances[i].getClass(), instances[i].getValue());

        }

    }

}

我希望這有幫助。


查看完整回答
反對 回復 2021-12-01
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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