反射常用的泛型类
Class<
T>
Constructor<
T>
小案例:
package com.genericity.demo7;
public class Person<T> {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.genericity.demo7;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class Test3 {
public static void main(String[] args) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
//如果不使用泛型
// Class personClass = Person.class;
// Constructor constructor = personClass.getConstructor();
// Object o = constructor.newInstance();
//使用泛型的好处
Class<Person> personClass = Person.class;
Constructor<Person> constructor = personClass.getConstructor();
Person person = constructor.newInstance();
}
}
工作建议:在实际的工作中,我们尽量不要使用泛型数组,建议使用泛型集合。