Home > Back-end >  How do you get reflection generic please
How do you get reflection generic please

Time:12-15

//the superclass Person
 
Public class Person {

Public T age;

Public Person (T age) {this. Age=age; }
}

//subclasses Student provides type
 
Public class Student extends Person {

Public Student (Integer age) {super (age); }
}

 
Public class ReflectDemo {

Public static void main (String [] args) throws the Exception {
List Stu=fun (a Student in the class, "1, 2");
System. The out. Println (stu);
}

Public static & lt; T> List Fun (Class Clazz, String seriable) throws the Exception {
//try to create two student object through reflection
List Stus=new ArrayList<> (a);
String [] which=seriable. Split (", ");

For (String age: which) {
//the following if you can not write an Integer. The class, but through the clazz reflection, but I can't get is the Object type
T stu=clazz. GetDeclaredConstructor (Integer. The class). NewInstance (Integer. The valueOf (age));
Stus. Add (stu);
}

Return stus;
}
}
  • Related