【问题标题】:How to check class validity with generic class如何使用泛型类检查类有效性
【发布时间】:2018-10-22 13:25:15
【问题描述】:

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

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

感谢您的帮助。

【问题讨论】:

  • 我在下面的评论中发布了代码
  • .add(((types[i]) Base) - 在这种情况下,Base 是什么意思?我假设您只想使用 types[i] 并在其上调用构造函数(或任何 getInstance() 的意思 - 你是说 Class 中的 newInstance(params) 吗?)

标签: java class oop object


【解决方案1】:

要检查有效性,试试这个

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

【讨论】:

    【解决方案2】:

    如果我正确阅读了这个问题,您希望创建一些派生类的实例,它们都具有相同的构造函数参数。如果是这种情况,那么您需要为每个派生类提供相同的构造函数(它不需要在基类中)并使用 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());
            }
        }
    }
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      感谢您帮助检查有效性(它有效!)但我仍然没有得到这个 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 转换为适当的类型。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-18
        • 1970-01-01
        • 1970-01-01
        • 2018-11-23
        • 1970-01-01
        • 2016-05-13
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多