.class其实是在java运行时就加载进去的
getClass()是运行程序时动态加载的
下面以例子说明:
首先建一个基类Baseclass
package classyongfa;
public class Baseclass {
private String height;
public String getHeight()
{
return height;
}
public void setHeight(String height)
{
this.height=height;
}
下面是继承Baseclass类Extendclass
package classyongfa;
public class Extendclass extends Baseclass {
private String width;
public String getWidth()
{
return width;
}
public void setWidth(String width)
{
this.width=width;
}
public static void main(String[] arg0)
{
Baseclass baseclass1=new Extendclass();
Baseclass baseclass2=new Baseclass();
System.out.println(baseclass1.getClass().getSimpleName());//实际运行的是继承类Extendclass
System.out.println(baseclass2.getClass().getSimpleName());//实际运行的是Baseclass
System.out.println(Baseclass.class.getSimpleName());//加载时类名
System.out.println(Extendclass.class.getSimpleName());//加载时类名
}
结果是
Extendclass
Baseclass
Baseclass
Extendclass