JavaSE-24.2.1【反射、获取Class类对象的三种方式】

 

 

 

复制代码
 1 package day15.lesson2;
 2 
 3 /*
 4 2 反射
 5 
 6 2.1 反射概述
 7     是指在运行时去获取一个类的变量和方法信息。然后通过获取到的信息来创建对象,调用方法的一种机制。
 8     由于这种动态性,可以极大的增强程序的灵活性,程序不用在编译期就完成确定,在运行期仍然可以扩展。
 9 10 
11 2.2 获取Class类对象的三种方式
12 
13     要想通过反射使用一个类,首先要获取到该类的字节码对象文件,即类型为Class类型的对象
14 
15     (1)类名.class属性
16         使用类的class属性来获取该类对应的Class对象。例如:Student.class返回Student类对应的Class对象
17     (2)对象名.getClass()方法
18         调用对象的getClass()方法,返回该对象所属类对应的Class对象
19         该方法是Object类中的方法,所有java对象都可以调用该方法
20     (3)Class.forName(全类名)方法
21         使用Class类中的静态方法forName(String className)
22         该方法需要传入字符串参数,该字符串的值为某类的全路径,即包含完整包名的路径
23 
24  */
25 public class Demo1Reflect {
26     public static void main(String[] args) throws ClassNotFoundException {
27         Class<Student> c1 = Student.class;
28         System.out.println(c1); //class day15.lesson2.Student
29         Class<Student> c2 = Student.class;
30         System.out.println(c2); //class day15.lesson2.Student
31         System.out.println(c1 == c2); //true
32 
33         System.out.println();
34 
35         Student s = new Student();
36         Class<? extends Student> c3 = s.getClass();
37         System.out.println(c3); //class day15.lesson2.Student
38         System.out.println(c1 == c3); //true
39 
40         System.out.println();
41 
42         Class<?> c4 = Class.forName("day15.lesson2.Student"); //抛出异常ClassNotFoundException
43         System.out.println(c4); //class day15.lesson2.Student
44         System.out.println(c1 == c4); //true
45     }
46 }
47 
48 class Student{
49     //成员变量
50     private String name; //私有
51     int age; //默认
52     public String address; //公共
53 
54     //构造方法
55     public Student() { //公共
56     }
57 
58     public Student(String name, int age, String address) { //公共
59         this.name = name;
60         this.age = age;
61         this.address = address;
62     }
63 
64     private Student(String name){ //私有
65         this.name = name;
66     }
67 
68     Student(String name, int age){ //默认
69         this.name = name;
70         this.age = age;
71     }
72 
73     //成员方法
74     private void function(){ //私有
75         System.out.println("function");
76     }
77 
78     public void method1(){ //公共
79         System.out.println("method");
80     }
81 
82     public void method2(String s){ //公共
83         System.out.println("method:" + s);
84     }
85 
86     public String method3(String s, int i){ //公共
87         return s + "," + i;
88     }
89 
90     @Override
91     public String toString() { //公共
92         return "Student{" +
93                 "name='" + name + '\'' +
94                 ", age=" + age +
95                 ", address='" + address + '\'' +
96                 '}';
97     }
98 }
复制代码

 

posted @   yub4by  阅读(63)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示