JAVA中getClass()以及getName()方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z249486888/article/details/83317657

getClass

public final Class<?> getClass()

返回此 Object 的运行时类。返回的 Class 对象是由所表示类的 static synchronized 方法锁定的对象。

Java的引用变量有两个类型,编译时类型和运行时类型。编译时类型由声明该变量时使用的类型决定,运行时类型由实际赋给该变量的对象决定。

public class StudentDemo {
	public static void main(String[] args) {
		Student ss = new StudentSon(1, 2);
	    Class a = ss.getClass();
		System.out.println(a.getName());
	}
}

getName

public String getName()  

以 String 的形式返回此 Class 对象所表示的实体(类、接口、数组类、基本类型或 void)名称。

实体:对象引用的目的地

该类运行时类型为StudentSon 编译时类型为Student  ,则ss.getClass()返回StudentSon类

运行结果:

StudentSon

猜你喜欢

转载自blog.csdn.net/z249486888/article/details/83317657
今日推荐