【案例1】通过一个对象获得完整的包名和类名
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package
Reflect;
/**
* 通过一个对象获得完整的包名和类名
* */
class
Demo{
//other codes...
}
class
hello{
public
static
void
main(String[] args) {
Demo demo=
new
Demo();
System.out.println(demo.getClass().getName());
}
}
|
【运行结果】:Reflect.Demo
添加一句:所有类的对象其实都是Class的实例。
【案例2】实例化Class类对象
1
2
3
4
5
6
|