super.getClass()與this.getClass(),this.getclass

來源:互聯網
上載者:User

super.getClass()與this.getClass(),this.getclass

原文地址:http://leihuang.org/2014/11/14/getClass-method/


首先看一段代碼:

import java.util.Date;public  class Test extends Date{    public static void main(String[] args) {        new Test().test();    }    public void test(){        System.out.println(super.getClass().getName());    }}

上面這段代碼的輸出為:Test

可能你會奇怪為什麼輸出的是Test,而不是Date呢?我明明是調用的super.getClass()啊。我們先不急,先來瞭解一下getClass()方法的機制是什麼。下面時getClass()在Object類中實現的源碼。

Returns the runtime classof 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.

public final native Class<?> More ...getClass();

從上面我們可以得知兩點:

  1. getClass()方法: 返回此 Object 的運行時類。返回的 Class 對象是由所表示類的 static synchronized 方法鎖定的對象。
  2. getClass()時final方法,子類無法重寫。

第一點我們可以得出:

什麼是運行時類,運行時類是Object的一個執行個體,注意了,關鍵來了,他返回的不是Object.class,他返回的是運行時類,就是虛擬機器中是誰在運行就是誰,如果你new Date(),Date當然是運行時類,而不是Object,否則所有類的getClass()方法都返回了Object.class 了。

根據上一段解釋,Date是Object的子類,Date.getClass()返回的肯定是Date.class,

同樣的Test繼承Date,假如有一個屬於Date的getClass()他返回的也不可能是Date.class,因為當new Test()後,Test是一個運行時類,只不過他擁有Date的資源結構。所以誰被執行個體化,誰就是一個運行時類。

第二點我們可以得出

上面我們討論了,Object中getClass()方法返回的是運行時類對象,誰被執行個體化,getClass()就得到誰。但那隻是Object中的getClass()方法而已,而我們時調用的Date.getClass(),or Test.getClass().

這裡就涉及到getClass()方法在Object中時final方法了,因此子類是無法重寫getClass方法的,所以不管是Date.getClass()還是Test.getClass(),其實調用的都是Object中的getClass()方法,這樣就保證了會遵循第一點。

如果想要從Test中得到Date.class,可以用Test.getClass().getSuperClass();

2014-11-14 12:06:54

Brave,Happy,Thanksgiving !


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.