簡體   English   中英

java類初始化說明

[英]java class initialization explanation

我是Java的新手,如果有人可以向我解釋以下代碼示例,我將非常高興。這只是一個示例Java代碼段。 但是主要的問題是,如果Learn類使用參數也是Object類來初始化另一個Smart類,那么在類Object Sample中添加點類會使我感到困惑。 任何解釋將不勝感激。 抱歉,這是一個基本問題。 謝謝。

class Learn {
//some codes 
Smart smart = new Smart(Sample.class);
//some codes
} 

Sample是類的名稱。 不是對象。 一個new Sample()是一個對象,其類為Sample Sample.class是一個對象,其類為java.lang.Class ,它描述類Sample

我將通過以下方式打破您的榜樣。

學習-是一Class

smart-Smart類型的Object Reference Variable ,可以說Class Learn具有Smart類型的引用。

Sample.class-一種獲取特定類型的Class<T>的方法。

從Java文檔中提取。

During implementation it depends on the Targeting bytecode version. If -target 1.4 (or During implementation it depends on the Targeting bytecode version. If -target 1.4 (or below), a call to Class.forName() is inserted into your code in a static method which is called during type initialization. If you use -target 1.5 (or above) the constant pool below), a call to Class.forName() is inserted into your code in a static method which is called during type initialization. If you use -target 1.5 (or above) the constant pool called during type initialization. If you use -target 1.5 (or above) the constant pool gets a "class" entry

有關更多詳細信息,請參閱Java語言規范的15.8.2節。

  1. 在Java中,有一個名為“ Class”的類,它表示類和接口。
  2. 有幾種方法可以獲取類“ Class”的實例。 請看一下java.lang.Class文檔。
    • Class.forName(String className)
    • obj.getClass()-obj是任何類實例
    • Sample.class -Sample是一個類
  3. 您正在使用第3種方法來獲取類“ Sample”的實例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM