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

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

    java變量的初始化及賦值
    http://3aj.cn/article/1612.html  回復  更多評論
      
    主站蜘蛛池模板: 亚洲好看的理论片电影| 亚洲国产精华液网站w| 67pao强力打造高清免费| 免费人成动漫在线播放r18| 亚洲人成网国产最新在线| 亚洲AV无一区二区三区久久| 免费精品无码AV片在线观看| 国产精品小视频免费无限app| 亚洲成AV人在线播放无码 | 成人福利免费视频| 日本人成在线视频免费播放| eeuss草民免费| 男女男精品网站免费观看| 在线视频亚洲一区| 亚洲精品无码高潮喷水A片软| 国产亚洲精品国看不卡| 久草视频免费在线| 免费国产黄网站在线观看视频| 亚洲乱码日产精品一二三| 亚洲AV无码国产精品色| 亚洲色av性色在线观无码| 国产亚洲一区二区精品| 国产午夜亚洲精品国产成人小说| 8x8x华人永久免费视频| 99国产精品视频免费观看| 亚洲av无码一区二区三区在线播放| 亚洲精品无码不卡在线播HE| 国产亚洲精品资在线| 亚洲一区二区三区乱码A| 免费人成视频在线| 妞干网免费视频在线观看| 最近中文字幕免费mv视频7| 久久久久免费看黄A片APP| 日韩免费精品视频| 我想看一级毛片免费的| 成人性生活免费视频| 日本无吗免费一二区| 67pao强力打造国产免费| 一区二区三区免费精品视频| 国产成人精品免费大全| 成人爽a毛片免费|