Java Dream  
          日歷
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789
          統計
          • 隨筆 - 2
          • 文章 - 1
          • 評論 - 1
          • 引用 - 0

          導航

          常用鏈接

          留言簿

          隨筆檔案

          文章檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           
            我們說構造器是一種方法,就象講澳大利亞的鴨嘴獸是一種哺育動物。(按:老外喜歡打比喻,我也就照著翻譯)。要理解鴨嘴獸,那么先必須理解它和其他哺育動物的區別。同樣地,要理解構造器,那么就要了解構造器和方法的區別。所有學習java的人,尤其是對那些要認證考試的,理解構造器是非常重要的。下面將簡單介紹一下 ,最后用一個表作了些簡單的總結。
           
          功能和作用的不同

            構造器是為了創建一個類的實例。這個過程也可以在創建一個對象的時候用到:Platypus p1 = new Platypus();
            
            相反,方法的作用是為了執行java代碼。
            
            修飾符,返回值和命名的不同
            構造器和方法在下面三個方便的區別:修飾符,返回值,命名。和方法一樣,構造器可以有任何訪問的修飾: public, protected, private或者沒有修飾(通常被package 和 friendly調用). 不同于方法的是,構造器不能有以下非訪問性質的修飾: abstract, final, native, static, 或者 synchronized。
            
            返回類型也是非常重要的。方法能返回任何類型的值或者無返回值(void),構造器沒有返回值,也不需要void。
            
            最后,談談兩者的命名。構造器使用和類相同的名字,而方法則不同。按照習慣,方法通常用小寫字母開始,而構造器通常用大寫字母開始。構造器通常是一個名詞,因為它和類名相同;而方法通常更接近動詞,因為它說明一個操作。
            
            "this"的用法
            構造器和方法使用關鍵字this有很大的區別。方法引用this指向正在執行方法的類的實例。靜態方法不能使用this關鍵字,因為靜態方法不屬于類的實例,所以this也就沒有什么東西去指向。構造器的this指向同一個類中,不同參數列表的另外一個構造器,我們看看下面的代碼:
            
            public class Platypus {
            
            String name;
            
            Platypus(String input) {
            name = input;
            }
            
            Platypus() {
            this("John/Mary Doe");
            }
            
            public static void main(String args[]) {
            Platypus p1 = new Platypus("digger");
            Platypus p2 = new Platypus();
            }
            }
            
            在上面的代碼中,有2個不同參數列表的構造器。第一個構造器,給類的成員name賦值,第二個構造器,調用第一個構造器,給成員變量name一個初始值 "John/Mary Doe".
            
            在構造器中,如果要使用關鍵字this,那么,必須放在第一行,如果不這樣,將導致一個編譯錯誤。

          "super"的用法

          構造器和方法,都用關鍵字super指向超類,但是用的方法不一樣。方法用這個關鍵字去執行被重載的超類中的方法。看下面的例子:
            
            class Mammal {
            void getBirthInfo() {
            System.out.println("born alive.");
            }
            }
            
            class Platypus extends Mammal {
            void getBirthInfo() {
            System.out.println("hatch from eggs");
            System.out.print("a mammal normally is ");
            super.getBirthInfo();
            }
            }
            
            在上面的例子中,使用super.getBirthInfo()去調用超類Mammal中被重載的方法。
            
            構造器使用super去調用超類中的構造器。而且這行代碼必須放在第一行,否則編譯將出錯。看下面的例子:
            
            public class SuperClassDemo {
            SuperClassDemo() {}
            }
            
            class Child extends SuperClassDemo {
            Child() {
            super();
            }
            }
            
            在上面這個沒有什么實際意義的例子中,構造器 Child()包含了 super,它的作用就是將超類中的構造器SuperClassDemo實例化,并加到 Child類中。
            
            編譯器自動加入代碼
            編譯器自動加入代碼到構造器,對于這個,java程序員新手可能比較混淆。當我們寫一個沒有構造器的類,編譯的時候,編譯器會自動加上一個不帶參數的構造器,例如:public class Example {}
            編譯后將如下代碼:
            
            public class Example {
            Example() {}
            }
            
            在構造器的第一行,沒有使用super,那么編譯器也會自動加上,例如:
            
            public class TestConstructors {
            TestConstructors() {}
            }
            
            編譯器會加上代碼,如下:
            
            public class TestConstructors {
            TestConstructors() {
            super;
            }
            }
            
            仔細想一下,就知道下面的代碼
            
            public class Example {}
            
            經過會被編譯器加代碼形如:
            
            public class Example {
            Example() {
            super;
            }
            }
            
          繼承
            構造器是不能被繼承的。子類可以繼承超類的任何方法。看看下面的代碼:
            
            public class Example {
            public void sayHi {
            system.out.println("Hi");
            }
            
            Example() {}
            }
            
            public class SubClass extends Example {
            }
            
            類 SubClass 自動繼承了父類中的sayHi方法,但是,父類中的構造器 Example()卻不能被繼承。


          總結

          subject construct methods
          function creat a instance of class functional sentence
          modify can't use abstract,final,native,static or synchronized use all the modified symbols
          void type no void value,no void include void
          name  same with the class name,and begin with capital  whatever, begin with lowercase
          this varible refer to the another construct within a class refer to the instance of a class,can't be use in the static methods
          extends can't be extended can be extended
          the compiler automatic added  support (if not ) not support
          posted on 2010-03-23 16:44 PoeLeon 閱讀(73) 評論(0)  編輯  收藏
           
          Copyright © PoeLeon Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 德昌县| 塔河县| 峨眉山市| 阳曲县| 龙川县| 镇巴县| 蒙自县| 新乡市| 虎林市| 故城县| 马鞍山市| 绥阳县| 当雄县| 新疆| 简阳市| 城步| 铁岭市| 锦州市| 泽库县| 泽州县| 略阳县| 秦安县| 习水县| 玉环县| 凤城市| 临桂县| 彭山县| 呼伦贝尔市| 蒲城县| 越西县| 新河县| 黔西| 工布江达县| 伊宁县| 东乌珠穆沁旗| 庄河市| 泸溪县| 哈巴河县| 商河县| 修文县| 蕉岭县|