00110_Class类

1、Class 对象是在加载类时由 Java虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的;

2、获取Class对象的三种方式

  (1)方式一:通过Object类中的getObject()方法

Person p = new Person();
Class c = p.getClass();

  (2)方式二:通过 类名.class 获取到字节码文件对象(任意数据类型都具备一个class静态属性,看上去要比第一种方式简单)

Class c2 = Person.class;

  (3)方式三: 通过Class类中的方法(将类名作为字符串传递给Class类中的静态方法forName即可)

Class c3 = Class.forName("Person");

3、第三种和前两种的区别

  (1)前两种你必须明确Person类型;

  (3)后面是指定这种类型的字符串就行.这种扩展更强.我不需要知道你的类.我只提供字符串,按照配置文件加载就可以了。

4、代码演示

 1 package cn.gzdlh_01_Reflect;
 2 
 3 /*
 4  * 获取.class字节码文件对象的方式
 5  *         1:通过Object类中的getObject()方法
 6  *         2: 通过 类名.class 获取到字节码文件对象
 7  *         3: 反射中的方法,
 8  *             public static Class<?> forName(String className) throws ClassNotFoundException
 9  *             返回与带有给定字符串名的类或接口相关联的 Class 对象 
10  */
11 public class ReflectDemo {
12     public static void main(String[] args) throws ClassNotFoundException {
13         // 1: 通过Object类中的getObject()方法
14         // Person p1 = new Person();
15         // Class c1 = p1.getClass();
16         // System.out.println("c1 = "+ c1);
17 
18         // 2: 通过 类名.class 获取到字节码文件对象
19         // Class c2 = Person.class;
20         // System.out.println("c2 = "+ c2);
21 
22         // 3: 反射中的方法
23         Class c3 = Class.forName("cn.gzdlh_01_Reflect.Person");// 包名.类名
24         System.out.println("c3 = " + c3);
25     }
26 }

  Person类

 1 package cn.gzdlh_01_Reflect;
 2 
 3 public class Person {
 4     // 成员变量
 5     public String name;
 6     public int age;
 7     private String address;
 8 
 9     // 构造方法
10     public Person() {
11         System.out.println("空参数构造方法");
12     }
13 
14     public Person(String name) {
15         this.name = name;
16         System.out.println("带有String的构造方法");
17     }
18 
19     // 私有的构造方法
20     private Person(String name, int age) {
21         this.name = name;
22         this.age = age;
23         System.out.println("带有String,int的构造方法");
24     }
25 
26     public Person(String name, int age, String address) {
27         this.name = name;
28         this.age = age;
29         this.address = address;
30         System.out.println("带有String, int, String的构造方法");
31     }
32 
33     // 成员方法
34     // 没有返回值没有参数的方法
35     public void method1() {
36         System.out.println("没有返回值没有参数的方法");
37     }
38 
39     // 没有返回值,有参数的方法
40     public void method2(String name) {
41         System.out.println("没有返回值,有参数的方法 name= " + name);
42     }
43 
44     // 有返回值,没有参数
45     public int method3() {
46         System.out.println("有返回值,没有参数的方法");
47         return 123;
48     }
49 
50     // 有返回值,有参数的方法
51     public String method4(String name) {
52         System.out.println("有返回值,有参数的方法");
53         return "哈哈" + name;
54     }
55 
56     // 私有方法
57     private void method5() {
58         System.out.println("私有方法");
59     }
60 
61     @Override
62     public String toString() {
63         return "Person [name=" + name + ", age=" + age + ", address=" + address
64                 + "]";
65     }
66 }
原文地址:https://www.cnblogs.com/gzdlh/p/8159200.html