this.getClass()和super.getClass()
得到的是同一個(gè)類===>當(dāng)前運(yùn)行時(shí)類险领。
這里其實(shí)很好理解玫鸟,這里將getClass()在jdk11中的實(shí)現(xiàn)貼出轿曙,
/*
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修飾的挺身,即方法不能被繼承&&具體實(shí)現(xiàn)在native侯谁。
方法返回的是運(yùn)行時(shí)類的全限定名(包名.類名),所以返回的是com.xxx.Child章钾,與Parent無(wú)關(guān)墙贱,要返回Parent的名字需要寫super.getClass().getSuperclass()
。
static synchronized methods of the represented class贱傀,當(dāng)前類鎖所使用的class惨撇。
Class和Object
Class對(duì)象
Instances of the class Class represent classes and interfaces in a running Java application.
由.class文件經(jīng)歷classLoad后,存在于Java application府寒。
Object對(duì)象
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.
前者在實(shí)例化對(duì)象的時(shí)候使用魁衙,后者為實(shí)例化對(duì)象的root父類。
Class<Object> objectClass = Object.class;