如何使用泛型类检查类的有效性

我正在处理代码,我必须将基类转换为派生类,其中我有一组由基派生的泛型类型。


例如,我有 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

    }

}

谢谢您的帮助。


哈士奇WWW
浏览 134回答 3
3回答

一只斗牛犬

要检查有效性,试试这个if&nbsp;(types[i].getClass().isAssignableFrom(Base.class))

尚方宝剑之说

感谢您帮助检查有效性(它有效!)但我仍然没有得到这个 newInstance 创建,因为我必须从 .csv 文件中读取数据,而我的派生类实际上是原始类型(如 int、float 等)的“包装器” . 我应该使用方法 getInstance() 和 create(string s) 创建新对象,所以它看起来像这样:public static class Derived1 extends Base { //Integer wrapper&nbsp; &nbsp; Integer value;&nbsp; &nbsp; public Derived1(Integer value) {&nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; }&nbsp; &nbsp; private static Integer integer = new Integer();&nbsp; &nbsp; public static Integer getInstance(){&nbsp; &nbsp; &nbsp; &nbsp; return integer;&nbsp; &nbsp; }&nbsp; &nbsp; private Integer(){};&nbsp; &nbsp; public Base create(String s) {&nbsp; &nbsp; &nbsp; &nbsp; value = java.lang.Integer.parseInt(s);&nbsp; &nbsp; &nbsp; &nbsp; return this;&nbsp; &nbsp; }}而且我不知道如何使用 Class 转换为适当的类型。

慕工程0101907

如果我正确阅读了这个问题,那么您想创建一些派生类的实例,它们都具有相同的构造函数参数。如果是这种情况,那么您需要为每个派生类提供相同的构造函数(它不需要在基类中)并使用 Constructor.newInstance(parameters) 创建实例。此外,由于您要确保每个派生类扩展基类,因此您将需要使用 Class.isAssignableFrom(class)。例如,import java.lang.reflect.Constructor;public class SO52930530 {&nbsp; &nbsp; public abstract static class Base {&nbsp; &nbsp; &nbsp; &nbsp; public abstract <T> T getValue();&nbsp; &nbsp; }&nbsp; &nbsp; public static class Derived1 extends Base {&nbsp; &nbsp; &nbsp; &nbsp; String value;&nbsp; &nbsp; &nbsp; &nbsp; public Derived1(String value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.value = value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public <T> T getValue() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static class Derived2 extends Base {&nbsp; &nbsp; &nbsp; &nbsp; Integer value;&nbsp; &nbsp; &nbsp; &nbsp; public Derived2(String value) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.value = new Integer(value);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; public <T> T getValue() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String... args) throws Exception {&nbsp; &nbsp; &nbsp; &nbsp; Class<? extends Base>[] extensions = new Class[]{Derived1.class, Derived2.class};&nbsp; &nbsp; &nbsp; &nbsp; String[] values = new String[]{"a", "1"};&nbsp; &nbsp; &nbsp; &nbsp; Base[] instances = new Base[values.length];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < instances.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class extension = extensions[i];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Base.class.isAssignableFrom(extension)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Constructor constructor = extension.getConstructor(String.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; instances[i] = (Base) constructor.newInstance(values[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < instances.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.printf("%d %s %s\n", i, instances[i].getClass(), instances[i].getValue());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}我希望这有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java