.class, class.forName(), getClass()的区别

类名.class叫做“类字面量”,因class是关键字, 所以类名.class编译时确定。

getclass()运行时根据实际实例确定,getClass()是动态而且是final的。

String.class 是能对类名的引用取得在内存中该类型class对象的引用,

new String().getClass() 是通过实例对象取得在内存中该实际类型class对象的引用。

 

它们和Class.forName():     (http://www.cnblogs.com/feiyun126/p/3229492.html)

1:Class cl=A.class;  

                  JVM将使用类A的类装载器, 将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作.返回类A的Class的对象。

2:Class cl=对象引用o.getClass();

                  返回引用o运行时真正所指的对象(因为:子对象的引用可能会赋给父对象的引用变量中)所属的类的Class的对象 。

3:Class.forName("类名");

                  .装入类A,并做类的初始化

.getClass()是动态的,其余是静态的。

.class和class.forName()只能返回类内field的默认值,getClass可以返回当前对象中field的最新值

Class.forName() 返回的是一个类,.newInstance() 后才创建一个对象,Class.forName()的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的.

待反射类:

 1 package yerasel;
 2 
 3 public class Person {
 4     private String name = "Alfira";
 5     public void getName() {
 6         System.out.println(name);
 7     }
 8     public void setName(String name, int a) {
 9         this.name = name + a;
10     }
11 }

反射代码:

 1 package yerasel;
 2 
 3 import java.lang.reflect.Method;
 4 
 5 public class Test {
 6 
 7     /**
 8      * @param args
 9      */
10     public static void main(String[] args) {
11         show("yerasel.Person");
12     }
13 
14     private static void show(String name) {
15         try {
16             // JVM将使用类A的类装载器,将类A装入内存(前提是:类A还没有装入内存),不对类A做类的初始化工作
17             Class classtype3 = Person.class;
18             // 获得classtype中的方法
19             Method getMethod3 = classtype3.getMethod("getName", new Class[] {});
20             Class[] parameterTypes3 = { String.class, int.class };
21             Method setMethod3 = classtype3
22                     .getMethod("setName", parameterTypes3);
23 
24             // 实例化对象,因为这一句才会输出“静态初始化”以及“初始化”
25             Object obj3 = classtype3.newInstance();
26             // 通过实例化后的对象调用方法
27             getMethod3.invoke(obj3); // 获取默认值
28             setMethod3.invoke(obj3, "Setting new ", 3); // 设置
29             getMethod3.invoke(obj3); // 获取最新
30             System.out.println("----------------");
31 
32             // 返回运行时真正所指的对象
33             Person p = new Person();
34             Class classtype = p.getClass();// Class.forName(name);
35             // 获得classtype中的方法
36             Method getMethod = classtype.getMethod("getName", new Class[] {});
37             Class[] parameterTypes = { String.class, int.class };
38             Method setMethod = classtype.getMethod("setName", parameterTypes);
39             getMethod.invoke(p);// 获取默认值
40             setMethod.invoke(p, "Setting new ", 1); // 设置
41             getMethod.invoke(p);// 获取最新
42             System.out.println("----------------");
43 
44             // 装入类,并做类的初始化
45             Class classtype2 = Class.forName(name);
46             // 获得classtype中的方法
47             Method getMethod2 = classtype2.getMethod("getName", new Class[] {});
48             Class[] parameterTypes2 = { String.class, int.class };
49             Method setMethod2 = classtype2
50                     .getMethod("setName", parameterTypes2);
51             // 实例化对象
52             Object obj2 = classtype2.newInstance();
53             // 通过实例化后的对象调用方法
54             getMethod2.invoke(obj2); // 获取默认值
55             setMethod2.invoke(obj2, "Setting new ", 2); // 设置
56             getMethod2.invoke(obj2); // 获取最新
57 
58             System.out.println("----------------");
59 
60         } catch (Exception e) {
61             System.out.println(e);
62         }
63     }
64 }

 

转载于:https://www.cnblogs.com/protected/p/6422793.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值