详解Java反射创建对象
作者:星夜孤帆 时间:2022-12-10 03:52:23
一、什么是反射
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()的返回结果同样也没有参数类型的过滤。
参考文章
来源:https://blog.csdn.net/qq_38826019/article/details/117385168
标签:Java,反射,创建对象
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C++集体数据交换实现示例讲解
2023-12-17 11:35:07
解决MyEclipse中的Building workspace问题的三个方法
2023-10-25 09:00:51
![](https://img.aspxhome.com/file/2023/2/81252_0s.png)
C#微信公众号开发 微信事件交互
2023-04-22 21:18:31
![](https://img.aspxhome.com/file/2023/7/91247_0s.png)
Android实现截图和分享功能的代码
2023-04-09 22:56:43
![](https://img.aspxhome.com/file/2023/8/106868_0s.png)
谈谈Java中Volatile关键字的理解
2021-07-08 04:49:13
Java 求解如何把二叉搜索树转换为累加树
2021-11-19 14:09:54
![](https://img.aspxhome.com/file/2023/8/61018_0s.png)
C#调用WebService实例开发
2022-11-21 22:51:08
![](https://img.aspxhome.com/file/2023/9/102209_0s.jpg)
Java中的synchronized关键字
2023-07-28 18:39:26
C#判断获取的是文件夹还是文件的实例
2022-11-25 05:00:01
Java实现生成JSON字符串的三种方式分享
2022-05-20 15:21:31
C# Aspose.Words 删除word中的图片操作
2023-07-29 12:01:43
![](https://img.aspxhome.com/file/2023/5/96325_0s.jpg)
关于C#中ajax跨域访问问题
2023-03-04 21:28:04
详解C#中Helper类的使用
2021-10-05 07:37:28
![](https://img.aspxhome.com/file/2023/7/108687_0s.png)
java自定义注解验证手机格式的实现示例
2023-06-24 10:42:44
SQL语句删除和添加外键、主键的方法
2023-04-16 22:18:35
C#实现过滤sql特殊字符的方法集合
2022-01-30 23:58:04
基于jQuery获取table数据发送到后端
2023-07-22 22:07:43
SpringBoot使用validation-api实现参数校验的示例
2022-12-02 14:33:40
![](https://img.aspxhome.com/file/2023/4/124714_0s.jpg)
在Spring Boot中使用Spring-data-jpa实现分页查询
2023-12-03 21:35:39
![](https://img.aspxhome.com/file/2023/5/89595_0s.png)
Android Studio中Logcat写入和查看日志
2021-08-05 12:37:18
![](https://img.aspxhome.com/file/2023/9/106019_0s.png)