当前位置:网站首页>动态创建对象执行方法
动态创建对象执行方法
2022-06-22 06:10:00 【酷小亚】
有了Class对象,能做什么?

调用指定的方法


setAccessible的作用:

代码如下:
public class Demo10 {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
Class c1 = Class.forName("www.jin.User");
//构造一个对象
User user = (User) c1.newInstance();//本质是调用了类的无参构造器
System.out.println(user);
System.out.println("-----------------------");
//通过构造器创建对象
Constructor constructor = c1.getDeclaredConstructor(String.class, int.class, int.class);
User user2 = (User) constructor.newInstance("酷小亚",001,14);
System.out.println(user2);
//通过反射调用普通方法
User user3 = (User) c1.newInstance();
//通过反射获取一个方法
Method setName = c1.getDeclaredMethod("setName", String.class);
//invoke : 激活的意思
//(对象,"方法的值")
setName.invoke(user3,"今宵");
System.out.println(user3.getName());
System.out.println("=======================");
//通过反射操作属性
User user4 = (User) c1.newInstance();
Field name = c1.getDeclaredField("name");
//不能直接操作私有属性,我们需要关闭程序的安全检测,属性或者方法的setAccessible(true)
name.setAccessible(true);
name.set(user4,"晋小");
System.out.println(user4.getName());
}
}
class User{
private String name;
private int id;
private int age;
public User() {
}
public User(String name, int id, int age) {
this.name = name;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", age=" + age +
'}';
}
}
运行结果:

边栏推荐
猜你喜欢

Discrete PID control based on MATLAB

W800芯片平台进入OpenHarmony主干

3D asset optimization and vertex data management for performance optimization

关于MNIST线性模型矩阵顺序问题

Unity app improves device availability

Single cell paper records (Part8) -- cell2location maps fine grained cell types in spatial transcriptomics

402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)

Configuration files required for SSM integration and error reports caused by common configuration errors

No business series 7: removing spots from old photos

D3D learning notes (1) - Introduction to the use conditions of autodraw at so stage
随机推荐
EMC的解决
Discrete PID control based on MATLAB
小熊派BearPi-HM Micro正式合入OpenHarmony主干
swagger常用注解汇总
Matlab system identification
[technical notes]
[technical notes]
【自己动手写CPU】异常相关指令的实现
Single cell thesis record (Part12) -- unsupervised spatial embedded deep representation of spatial transcriptomics
富设备平台突破:基于RK3568的DAYU200进入OpenHarmony 3.1 Release主干
401 string (344. reverse string, 541. reverse string II, Title: Sword finger offer 05. replace spaces, 151. reverse words in string)
[Key review of cloud computing]
D3D10 screenshot function saves texture to local
Unity app improves device availability
No business series 7: removing spots from old photos
【NAND文件系统】UBIFS介绍
3D asset optimization and vertex data management for performance optimization
Ptrade trading program code - from zero to firm offer 19
关于MNIST线性模型矩阵顺序问题
OSG compiling osgqt