<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 閱讀(2420) 評論(1)  編輯  收藏 所屬分類: Java編程 、C++編程

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

    java變量的初始化及賦值
    http://3aj.cn/article/1612.html  回復  更多評論
      
    主站蜘蛛池模板: 亚洲va无码专区国产乱码| 亚洲高清无码综合性爱视频| 久久亚洲精精品中文字幕| av网站免费线看| 亚洲色欲色欲www在线丝| 99亚洲乱人伦aⅴ精品| 国产精品免费小视频| 亚洲AV无码专区在线观看成人| 最近最新中文字幕完整版免费高清 | 久久精品国产亚洲精品2020| 国产中文字幕在线免费观看 | 久久青青草原亚洲AV无码麻豆 | 又大又硬又粗又黄的视频免费看| 国产小视频免费观看| 美女视频黄a视频全免费网站色| 日产国产精品亚洲系列| 一级毛片免费播放试看60分钟| 不卡精品国产_亚洲人成在线 | 中文字幕在线观看亚洲视频| 成人免费午夜在线观看| 亚洲精品GV天堂无码男同| 国产精品免费播放| 成人免费av一区二区三区| 亚洲日本中文字幕| 女人18毛片免费观看| 一级特黄色毛片免费看| 亚洲欧洲日产国码无码久久99| 无人在线观看免费高清| 亚洲一区欧洲一区| 亚洲黄黄黄网站在线观看| 鲁丝片一区二区三区免费| 亚洲中文字幕人成乱码| 国产一级做a爱免费视频| 97在线免费视频| 亚洲AV无码乱码在线观看代蜜桃| 四虎免费久久影院| 一级特黄aa毛片免费观看| 亚洲日韩精品无码专区加勒比| 狠狠亚洲狠狠欧洲2019| 波多野结衣在线免费视频 | 亚洲va在线va天堂va不卡下载|