<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  回復  更多評論
      
    主站蜘蛛池模板: 久久久久久久久久久免费精品| 亚洲AV无码国产一区二区三区| 99re6在线精品免费观看| 免费日本黄色网址| 美女被艹免费视频| 亚洲A丁香五香天堂网| 无码人妻一区二区三区免费视频 | 亚洲欧美在线x视频| 中文字幕人成无码免费视频| 亚洲国产精品免费在线观看| 0588影视手机免费看片| 亚洲男人电影天堂| 大地资源在线观看免费高清| 亚洲色大成网站www久久九| 国产精品久久香蕉免费播放 | 国产日韩久久免费影院| 亚洲日本va在线视频观看| 久久精品国产这里是免费| 久久亚洲sm情趣捆绑调教| 国产一卡二卡四卡免费| 亚洲精品成a人在线观看夫| 亚洲精品国产精品国自产观看| 成人无码WWW免费视频| 亚洲另类春色国产精品| 成人国产mv免费视频| 久久国产免费直播| 亚洲欧洲综合在线| 免费a级毛片永久免费| 美女巨胸喷奶水视频www免费| 亚洲伊人久久大香线蕉苏妲己| 免费人成网站在线观看10分钟| 亚洲AV成人无码网站| 亚洲日韩aⅴ在线视频| 国产精品久久久久久久久免费| 亚洲精品亚洲人成在线| 亚洲人成色7777在线观看| 妻子5免费完整高清电视| 一级A毛片免费观看久久精品| 亚洲一区影音先锋色资源| 免费v片视频在线观看视频| 久久成人免费播放网站|