码迷,mamicode.com
首页 > 其他好文 > 详细

反射之Class类的使用

时间:2017-06-03 21:48:24      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:format   orm   type   方法   one   test   log   anti   ace   

 任何一个类都是Class类的实例对象,这个实例对象有三种表示方式
  • 第一种表示方式(任何一个类都有一个隐含的静态成员变量class):

 1 Class c1 = Foo.class; 

  • 第二种表示方式(已知该类对象,通过getClass方法):

 1 Foo foo1 = new Foo(); 2 Class c2 = foo1.getClass(); 

  ※ c1、c2表示了Foo类的类类型(class type)
  • 第三种表示方式
1 Class c3 = null;
2 try {
3     c3 = Class.forName("com.format.test.Foo");
4 } catch (ClassNotFoundException e) {
5     e.printStackTrace();
6 }
  ※ 通过类的类型创建该类的对象实例
1 try {
2     Foo foo2 = (Foo) c1.newInstance(); //需要有无参构造
3 } catch (InstantiationException e) {
4     e.printStackTrace();
5 } catch (IllegalAccessException e) {
6     e.printStackTrace();
7 }

 

反射之Class类的使用

标签:format   orm   type   方法   one   test   log   anti   ace   

原文地址:http://www.cnblogs.com/format-ch/p/6938447.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!