Getclass():
返回此 Object
的运行时类。用的是谁的构造函数,返回的就是谁的类型。
package com.object;
publicclass Demo1 {
/**
* @param args
*/
publicstaticvoid main(String[] args) {
// TODOAuto-generated method stub
Person p = new Person("tom",22);
Object p2 = p.show();
System.out.println(p2);
Person p1 =(Person)p.show();//强转,调用子类的属性
System.out.println(p1.name);
System.out.println(p1.getClass());//返回p1的类,即返回值类型是类,不知道是什么类,就把类型写为class定义一个变量来接收,示例如下。(输出的结果是:class com.object.Person)
//Class c = p.getClass();
//System.out.println(c);
}
}
package com.object;
publicclass Person implements Cloneable {
String name;
intage;
Person(){
}
Person(String name,int age){
this.name = name;
this.age = age;
}
public Object show(){
try {
Object p1 = this.clone();
return p1;
} catch (CloneNotSupportedException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}
returnnull;
}
}