一、什么是反射
Java Reflaction in Action中的解释:反射是运行中的程序检查自己和软件运行环境的能力,它可以根据它发现的进行改变。通俗的讲就是反射可以在运行时根据指定的类名获得类的信息 个人理解:就是我们对于创建对象我们除了通过 new关键字创建外,还能通过什么创建呢?private的属属性真的不能获取吗?反射就能做到打破这些所谓的规则反射和new创建对象谁的效率高? new
二、通过类对象调用newInstance()方法,适用于无参构造方法
2.1 类名.class
public class Main {
public static void main(String[] args) throws IllegalAccessException, InstantiationException {
Class<Person> clazz = Person.class;
Person person = clazz.newInstance();
System.out.println(person instanceof Person); // true
}
}
class Person {
private Integer age;
private String name;
public Person() {
}
}
2.2 Class.forName
public class Main {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
Class<?> clazz = Class.forName("com.best.test.Person");
Person person = (Person) clazz.newInstance();
System.out.println(person instanceof Person); // true
}
}
class Person {
private Integer age;
private String name;
public Person() {
}
}
2.3 对象名.getClass
public class Main {
public static void main(String[] args) throws IllegalAccessException, InstantiationException{
Person person = new Person();
Class<? extends Person> clazz = person.getClass();
Person person1 = clazz.newInstance();
System.out.println(person1 instanceof Person); // true
}
}
class Person {
private Integer age;
private String name;
public Person() {
}
}
三、getConstructor()和getDeclaredConstructor()
通过类对象的getConstructor()或getDeclaredConstructor()方法获得构造器(Constructor)对象并调用其newInstance()方法创建对象,适用于无参和有参构造方法。
3.1 getConstructor()
public class Main {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<Person> clazz = Person.class;
Constructor<Person> ctor = clazz.getConstructor(Integer.class, String.class);
Person person = ctor.newInstance(26, "jak");
System.out.println(person instanceof Person); // true
}
}
class Person {
private Integer age;
private String name;
public Person(Integer age, String name) {
this.age = age;
this.name = name;
}
}
3.2 getDeclaredConstructor()
public class Main {
public static void main(String[] args) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
Class<Person> clazz = Person.class;
Constructor<Person> ctor = clazz.getDeclaredConstructor(String.class);
Person person = ctor.newInstance("jak");
System.out.println(person instanceof Person); // true
}
}
class Person {
private Integer age;
private String name;
public Person(Integer age, String name) {
this.age = age;
this.name = name;
}
public Person(String name) {
this.name = name;
}
}
3.3 getConstructor()和getDeclaredConstructor()区别
getDeclaredConstructor(Class<?>... parameterTypes)
这个方法会返回指定参数类型的所有构造器,包括public的和非public的,当然也包括private的。getDeclaredConstructors()的返回结果就没有参数类型的过滤了。
再来看getConstructor(Class<?>... parameterTypes)
这个方法返回的是上面那个方法返回结果的子集,只返回指定参数类型访问权限是public的构造器。getConstructors()的返回结果同样也没有参数类型的过滤。
参考文章
到此这篇关于详解Java反射创建对象的文章就介绍到这了,更多相关Java反射创建对象内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!