当前位置:网站首页>动态创建对象执行方法
动态创建对象执行方法
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 +
'}';
}
}
运行结果:

边栏推荐
- Shengxin visualization (Part4) -- correlation diagram
- TCP连接细节问题
- TCP connection details
- Single precision, double precision and precision (Reprint)
- 【自己动手写CPU】异常相关指令的实现
- Vulkan pre rotation processing equipment direction
- tab[i = (n - 1) & hash] 的详细解读
- Generics in C #
- Performance optimization best practices for reducing Game Size
- 生信可视化(part4)--相关性图
猜你喜欢

虚职、架空、拖后腿,大厂开源办公室到底什么样?

402-字符串(题目:剑指Offer58-II.左旋转字符串、 28. 实现 strStr()、459.重复的子字符串)

h = key. Hashcode()) ^ (H > > 16) detailed explanation and why the hashcode value should be shifted to the right by 16 bits and XOR with the original hashcode value

从入门到精通之专家系统CLIPS(一)CLIPS初识与概述

Little bear school bearpi HM micro officially integrated into openharmony trunk

EMC的解决

Shengxin visualization (Part2) -- box diagram

单细胞论文记录(part13)--SpaGCN: Integrating gene expression, spatial location and histology to ...

reduce_sum()中的reduction_indices

Discrete PID control based on MATLAB
随机推荐
信息系统项目管理 - 范围管理(划重点)
生信可视化(part3)--小提琴图
Discrete PID control based on MATLAB
Frame profiling
W800 chip platform enters openharmony backbone
tab[i = (n - 1) & hash] 的详细解读
BinaryFormatter saving and loading game data for unity
Linear regression: least squares, Tellson estimation, RANSAC
[technical notes]
TCP connection details
汇顶科技GR551x系列开发板已支持OpenHarmony
【雲計算重點複習】
Design input of Oracle project management system
reduce_ Reduction in sum()_ indices
401-字符串(344. 反转字符串、541. 反转字符串II、题目:剑指Offer 05.替换空格、151. 颠倒字符串中的单词)
[Examen des points clés de l'informatique en nuage]
402 string (Title: Sword finger offer58 ii. left rotation string, 28. implementation of strstr(), 459 Repeated substrings)
Shengxin visualization (Part4) -- correlation diagram
常用的辅助类—(重点)
Little bear school bearpi HM micro officially integrated into openharmony trunk