<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-204  評論-149  文章-0  trackbacks-0
     

    JAVA中子類對象的實例化過程

     Person.java

    class Person
    {
     
    public String name = "not know";//4
     public int age = -1 ;//5
     public Person()
     
    {    
     }
     
    public Person(String name , int age)//3
     {
      
    this.name = name ;//6
      this.age = age;//7
     }
     
    public void showInfo()
     
    {
      System.out.println("Name is :"+name+" Age is :"+age);
     }
    }
    class Student extends Person
    {
     
    public String school = "not konwn";//8
        public Student()
        
    {
         
    super();    
        }
        
    public Student (String name ,int age, String school)  //1
        {
         
    super(name,age);  //2
         this.school = school;//9
        }
        
    public void showInfo()
        
    {
         
    super.showInfo();
         System.out.println("School is :"+school);
        }
    }

    AllTest.java

    class AllTest
    {
     
    public static void main(String[]args)
     
    {
      Student stA = 
    new Student("wanghao",20,"qinghuadaxue"); //0
     }
    }

    其語句的執行順序是怎樣的呢?
    java
    中,在使用new操作符創建一個類的實例對象的時候,開始分配空間并將成員變量初始化為默認的數值,注意這里并不是指將變量初始化為在變量定義處的初始值,而是給整形賦值0,給字符串賦值null 這一點于C++不同,(student.name = null , student.age = 0
    然后在進入類的構造函數。

    在構造函數里面,首先要檢查是否有this或者super調用,this調用是完成本類本身的構造函數之間的調用,super調用是完成對父類的調用。二者只能出現一個,并且只能作為構造函數的第一句出現。在調用thissuper的時候實現程序的跳轉,轉而執行被調用的this構造函數或者super構造函數。
    thissuper執行完畢,程序轉而執行在類定義的時候進行的變量初始化工作
    這個執行完畢,才是構造函數中剩下的代碼的執行。
    執行順序已經用綠色標出。

    Order of initialization
    Within a class, the order of initialization is determined by the order that the variables are defined within the class. The variable definitions may be scattered throughout and in between method definitions, but the variables are initialized before any methods can be called—even the constructor. For example: Feedback
    //: c04:OrderOfInitialization.java
    // Demonstrates initialization order.
    import com.bruceeckel.simpletest.*;

    // When the constructor is called to create a
    // Tag object, you'll see a message:
    class Tag {
      Tag(int marker) {
        System.out.println("Tag(" + marker + ")");
      }
    }

    class Card {
      Tag t1 = new Tag(1); // Before constructor
      Card() {
        // Indicate we're in the constructor:
        System.out.println("Card()");
        t3 = new Tag(33); // Reinitialize t3
      }
      Tag t2 = new Tag(2); // After constructor
      void f() {
        System.out.println("f()");
      }
      Tag t3 = new Tag(3); // At end
    }

    public class OrderOfInitialization {
      static Test monitor = new Test();
      public static void main(String[] args) {
        Card t = new Card();
        t.f(); // Shows that construction is done
        monitor.expect(new String[] {
          "Tag(1)",
          "Tag(2)",
          "Tag(3)",
          "Card()",
          "Tag(33)",
          "f()"
        });
      }
    } ///:~


    //: c04:StaticInitialization.java
    // Specifying initial values in a class definition.
    class Bowl {
      Bowl(int marker) {
        System.out.println("Bowl(" + marker + ")");
      }
      void f(int marker) {
        System.out.println("f(" + marker + ")");
      }
    }

    class Table {
      static Bowl b1 = new Bowl(1);
      Table() {
        System.out.println("Table()");
        b2.f(1);
      }
      void f2(int marker) {
        System.out.println("f2(" + marker + ")");
      }
      static Bowl b2 = new Bowl(2);
    }

    class Cupboard {
      Bowl b3 = new Bowl(3);
      static Bowl b4 = new Bowl(4);
      Cupboard() {
        System.out.println("Cupboard()");
        b4.f(2);
      }
      void f3(int marker) {
        System.out.println("f3(" + marker + ")");
      }
      static Bowl b5 = new Bowl(5);
    }

    public class StaticInitialization {
      static Test monitor = new Test();
      public static void main(String[] args) {
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        System.out.println("Creating new Cupboard() in main");
        new Cupboard();
        t2.f2(1);
        t3.f3(1);
        monitor.expect(new String[] {
          "Bowl(1)",
          "Bowl(2)",
          "Table()",
          "f(1)",
          "Bowl(4)",
          "Bowl(5)",
          "Bowl(3)",
          "Cupboard()",
          "f(2)",
          "Creating new Cupboard() in main",
          "Bowl(3)",
          "Cupboard()",
          "f(2)",
          "Creating new Cupboard() in main",
          "Bowl(3)",
          "Cupboard()",
          "f(2)",
          "f2(1)",
          "f3(1)"
        });
      }
      static Table t2 = new Table();
      static Cupboard t3 = new Cupboard();
    } ///:~


    繼承情況下的初始化
    了解一下包括繼承在內的初始化的過程將是非常有益的,這樣就能有個總
    體的了解。看看下面這段代碼:
    //: c06:Beetle.java
    // The full process of initialization.
    class Insect {
      protected static Test monitor = new Test();
      private int i = 9;
      protected int j;
      Insect() {
        System.out.println("i = " + i + ", j = " + j);
        j = 39;
      }
      private static int x1 = print("static Insect.x1 initialized");
      static int print(String s) {
        System.out.println(s);
        return 47;
      }
    }

    public class Beetle extends Insect {
      private int k = print("Beetle.k initialized");
      public Beetle() {
        System.out.println("k = " + k);
        System.out.println("j = " + j);
      }
      private static int x2 = print("static Beetle.x2 initialized");
      public static void main(String[] args) {
        System.out.println("Beetle constructor");
        Beetle b = new Beetle();
        monitor.expect(new String[] {
          "static Insect.x1 initialized",
          "static Beetle.x2 initialized",
          "Beetle constructor",
          "i = 9, j = 0",
          "Beetle.k initialized",
          "k = 47",
          "j = 39"
        });
      }
    } ///:~

    當你用Java 運行Beetle 的時候,第一件事就是訪問了Beetel.main( )(這是一個static ),于是裝載器(loader)就會為

    你尋找經編譯的Beetle 類的代碼(也就是Beetle.class 文件)。在裝載的過程中,裝載器注意到它有一個基類(也就是extends 所要表示的意思),于是它再裝載基類。不管你創不創建基類對象,這個過程總會發生。(試試看,把創建對象的那句注釋掉,看看會有什么結果。)如果基類還有基類,那么這第二個基類也會被裝載,以此類推下一步,它會執行“根基類(root bas e class)(這里就是Insect)static 初始化,然后是下一個派生類的static 初始化,以此類推。這個順序非常重要,因為派生類的“靜態初始化(即前面講的static 初始化)”有可能要依賴基類成員的正確初始化。

    現在所有必要的類都已經裝載結束,可以創建對象了。首先,對象里的所有的primitive 都會被設成它們的缺省值,而reference 也會被設成null——這個過程是一瞬間完成的,對象的內存會被統一地設置成“兩進制的零(binary zero)”。如果有對成員變量賦初值,則對成員變量進行賦值,然后調用基類的構造函數。調用是自動發生的,但是你可以使用super 來指定調用哪個構造函數(也就Beetle( )構造函數所做的第一件事)。基類的構造過程以及構造順序,同派生類的相同。基類構造函數運行完畢之后,會按照各個變量的字面順序進行初始化。最后會執行構造函數的其余部分。

    posted on 2009-05-11 21:16 Frank_Fang 閱讀(2413) 評論(1)  編輯  收藏 所屬分類: Java編程C++編程

    評論:
    # re: java對象的實例化過程,成員變量的初始化順序 2013-10-30 21:34 | 3a教程網

    java變量的初始化及賦值
    http://3aj.cn/article/1612.html  回復  更多評論
      
    主站蜘蛛池模板: 国产卡二卡三卡四卡免费网址| 四虎影视永久免费观看地址| 亚洲日韩国产一区二区三区在线| 日本中文一区二区三区亚洲| 人人玩人人添人人澡免费| 亚洲AV无码乱码在线观看代蜜桃 | 免费精品国产日韩热久久| 亚洲国产日韩a在线播放| 亚洲欧洲国产精品香蕉网| 中文字幕无码成人免费视频| 亚洲日韩在线观看免费视频| 亚洲最大在线视频| 亚洲国产综合精品中文字幕 | 亚洲精品麻豆av| 亚洲成人免费电影| 一区二区三区视频免费观看| 亚洲av永久无码精品天堂久久| 亚洲精品尤物yw在线影院| 精品成在人线AV无码免费看 | 久久免费看少妇高潮V片特黄| 亚洲精品国产摄像头| 少妇中文字幕乱码亚洲影视| 亚洲国产精品成人AV无码久久综合影院| 95老司机免费福利| 国产精品无码永久免费888| 亚洲精品无码aⅴ中文字幕蜜桃| 亚洲AV无码成人精品区在线观看| 日韩精品成人亚洲专区| 免费电视剧在线观看| 七色永久性tv网站免费看| 牛牛在线精品观看免费正| 亚洲性无码一区二区三区| 久久精品蜜芽亚洲国产AV| 亚洲狠狠婷婷综合久久久久| 国产一级淫片视频免费看| 99久久综合国产精品免费| 日韩人妻无码精品久久免费一| 一区二区3区免费视频| 亚洲日韩在线中文字幕综合| 波多野结衣亚洲一级| 亚洲网站视频在线观看|