BloveSaga

          在希臘帕爾納斯山南坡上,有一個馳名世界的戴爾波伊神托所,在它的入口處的巨石上赫然銹刻著這樣幾個大字: 認識你自己!

            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
            34 隨筆 :: 12 文章 :: 122 評論 :: 0 Trackbacks

          ?? 理解繼承是理解面向?qū)ο蟪绦蛟O計的關鍵.在Java中,通過關鍵字extends繼承一個已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類).在Java中不允許多繼承.code:
          class Animal
          {
          ?int height,weight;
          ?void eat()
          ?{
          ??System.out.println("Animal eat!");
          ?}
          ?void sleep()
          ?{
          ??System.out.println("Animal sleep!");
          ?}
          ?void breathe()
          ?{
          ??System.out.println("Animal breathe!");
          ?}
          }
          class Fish extends Animal
          {
          ?
          }
          class DoMain
          {
          ?public static void main(String[] args)
          ?{
          ??Animal an=new Animal();
          ??Fish fn=new Fish();
          ??
          ??an.breathe();
          ??fn.breathe();
          ??fn.height=30;
          ??fn.weight=20;
          ?}
          }
          Result:
          F:\Java Develop>javac Animal.java

          F:\Java Develop>java DoMain
          Animal breathe!
          Animal breathe!
          (這說明派生類繼承了父類的所有方法和成員變量.)

          方法的覆蓋(override)
          在子類中定義一個與父類同名,返回類型,參數(shù)類型均相同的一個方法,稱為方法的覆蓋,方法的覆蓋發(fā)生在子類與父類之間.
          code:
          class Animal
          {
          ?int height,weight;
          ?void eat()
          ?{
          ??System.out.println("Animal eat!");
          ?}
          ?void sleep()
          ?{
          ??System.out.println("Animal sleep!");
          ?}
          ?void breathe()
          ?{
          ??System.out.println("Animal breathe!");
          ?}
          }
          class Fish extends Animal
          {
          ?int weight,height;?? //隱藏了父類的weight,height;
          ?void breathe()? //override method breathe()
          ?{
          ??super.breathe();? //用super調(diào)用父類的構造方法
          ??System.out.println("Fish bubble");
          ?}
          }
          class DoMain
          {
          ?public static void main(String[] args)
          ?{
          ?//?Animal an=new Animal();
          ??Fish fn=new Fish();
          ??
          ??an.breathe();
          ??fn.breathe();
          ??fn.height=30;
          ??fn.weight=20;
          ?}
          }
          輸出結果:
          F:\Java Develop>javac Animal.java

          F:\Java Develop>java DoMain
          Animal breathe!
          Fish bubble

          特殊變量super
          * 使用特殊變量super提供對父類的訪問
          * 可以使用super訪問父類被子類隱藏的變量或覆蓋的方法
          * 每個子類構造方法的第一條語句都是隱含的調(diào)用super,如果父類沒有這種形式的構造函數(shù)就會報錯.
          code:
          class Animal
          {
          ?int height,weight;
          ?Animal()
          ?{
          ??System.out.println("Animal construct");
          ?}
          ?void eat()
          ?{
          ??System.out.println("Animal eat!");
          ?}
          ?void sleep()
          ?{
          ??System.out.println("Animal sleep!");
          ?}
          ?void breathe()
          ?{
          ??System.out.println("Animal breathe!");
          ?}
          }

          class Fish extends Animal
          {
          ?Fish()
          ?{
          ??System.out.println("Fish construct");
          ?}
          ?void breathe()? //override method breathe()
          ?{
          ??System.out.println("Fish bubble");
          ?}
          }
          class DoMain
          {
          ?public static void main(String[] args)
          ?{
          ??//Animal an=new Animal();
          ??Fish fn=new Fish();
          ??
          ??//an.breathe();
          ??//fn.breathe();
          ??//fn.height=30;
          ??//fn.weight=20;
          ?}
          }
          輸出結果:
          F:\Java Develop>javac Animal.java

          F:\Java Develop>java DoMain
          Animal construct
          Fish construct

          posted on 2006-06-04 11:18 藍色Saga 閱讀(247) 評論(0)  編輯  收藏 所屬分類: Basic Study for JAVA
          主站蜘蛛池模板: 苏尼特右旗| 昭苏县| 湟中县| 南华县| 香港| 沂南县| 若羌县| 招远市| 乌鲁木齐县| 武隆县| 邯郸县| 休宁县| 民勤县| 桐乡市| 巩义市| 漯河市| 民乐县| 张掖市| 昌图县| 温州市| 寻乌县| 怀远县| 同江市| 阜平县| 广宗县| 晋中市| 山西省| 共和县| 陆丰市| 苏尼特左旗| 金湖县| 视频| 云梦县| 奉化市| 麟游县| 泗阳县| 永嘉县| 双峰县| 中宁县| 上栗县| 东兴市|