JDK 1.5編譯中的一個奇怪問題

來源:互聯網
上載者:User

學員今天中午問了我一個有趣的Java編譯問題,我也無法給他解釋,不知道有沒有路過的高人能夠解釋清楚原因,望不吝賜教!

   下面程式的main方法中的第二行代碼和注釋中的兩行代碼錶達的意思完全相同,注釋中的兩行代碼不能通過編譯(這很容易理解),而第二行(採用方法調用鏈)卻可以順利通過編譯(這就很難理解了)。

public class Test
{
  public void func()
  {
   System.out.println("func");
  }

 public static void main(String args[]) throws Exception
  {
   Object obj = new Test();
   //下面這行可以成功編譯
   ((Test)obj).getClass().newInstance().func();

//下面這兩行無法通過編譯
   /*Class c = ((Test)obj).getClass();
   c.newInstance().func(); */
  }
}

感謝paulex先生的協助,在paulex先生的提示下,我基本上明白了上述問題的原因。下面是paulex先生的解答:

因為Generic, 編譯器可以在編譯期獲得類型資訊所以可以編譯這類代碼。你將下面那兩行改成

Class<? extends Test> c = ((Test)obj).getClass();
c.newInstance().func();

應該就能通過編譯了。

下面是我在paulex先生解答的基礎上,對問題的進一步解釋:

在JDK 1.5中引入範型後,Object.getClass()方法的定義如下:

public final Class<? extends Object> getClass()
Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.

Returns:
The java.lang.Class object that represents the runtime class of the object. The result is of type Class<? extends X> where X is the erasure of the static type of the expression on which getClass is called.

這說明((Test)obj).getClass()語句返回的物件類型為Class<? extends Test>,而Class<T>的newInstance()方法的定義如下:

public T newInstance() throws InstantiationException,IllegalAccessException

即對於編譯器看來,Class<Test>的newInstance()方法的物件類型為Test,而((Test)obj).getClass()返回的為物件類型為Class<? extends Test>,所以,編譯器認為((Test)obj).getClass().newInstance()返回的物件類型為Test。

下面這兩行代碼之所以無法通過編譯

Class c = ((Test)obj).getClass();
c.newInstance().func();

是因為((Test)obj).getClass()返回的為物件類型為Class<? extends Test>,但是我們在第一行將結果強制轉換成了Class,然後再去調用Class的newInstance方法,而不是去調用Class<Test>的newInstance方法,編譯器當然不再認為Class的newInstance方法返回的對象為Test了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.