this.getClass()和super.getClass()
得到的是同一个类===>当前运行时类。
这里其实很好理解,这里将getClass()在jdk11中的实现贴出,
/*
Returns the runtime class of this Object. The returned Class object is the object that is locked by static
synchronized methods of the represented class.
The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression
on which getClass is called. For example, no cast is required in this code fragment:
Number n = 0; Class<? extends Number> c = n.getClass();
Returns:
The Class object that represents the runtime class of this object.
*/
@HotSpotIntrinsicCandidate
public final native Class<?> getClass();
该方法定义在Object类中,由final和native修饰的,即方法不能被继承&&具体实现在native。
方法返回的是运行时类的全限定名(包名.类名),所以返回的是com.xxx.Child,与Parent无关,要返回Parent的名字需要写super.getClass().getSuperclass()
。
static synchronized methods of the represented class,当前类锁所使用的class。
Class和Object
Class对象
Instances of the class Class represent classes and interfaces in a running Java application.
由.class文件经历classLoad后,存在于Java application。
Object对象
Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.
前者在实例化对象的时候使用,后者为实例化对象的root父类。
Class<Object> objectClass = Object.class;