理解繼承是理解面向?qū)ο蟪绦蛟O(shè)計(jì)的關(guān)鍵.在Java中,通過關(guān)鍵字extends繼承一個(gè)已有的類,被繼承的類稱為父類(超類,基類),新的類稱為子類(派生類).在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)
          在子類中定義一個(gè)與父類同名,返回類型,參數(shù)類型均相同的一個(gè)方法,稱為方法的覆蓋,方法的覆蓋發(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)用父類的構(gòu)造方法
          ? 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;
          ?}
          }
          輸出結(jié)果:
          F:\Java Develop>javac Animal.java

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

          特殊變量super
          * 使用特殊變量super提供對(duì)父類的訪問
          * 可以使用super訪問父類被子類隱藏的變量或覆蓋的方法
          * 每個(gè)子類構(gòu)造方法的第一條語句都是隱含的調(diào)用super,如果父類沒有這種形式的構(gòu)造函數(shù)就會(huì)報(bào)錯(cuò).
          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;
          ?}
          }
          輸出結(jié)果:
          F:\Java Develop>javac Animal.java

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



          ?通過覆蓋父類的方法來實(shí)現(xiàn),在運(yùn)行時(shí)根據(jù)傳遞對(duì)象的引用,來調(diào)用相應(yīng)的方法.
          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
          {
          ?static void fn(Animal an)
          ?{
          ? an.breathe();
          ?}
          ?public static void main(String[] args)
          ?{
          ? //Animal an=new Animal();
          ? Fish fh=new Fish();
          ? Animal an;
          ? an=fh;
          ? DoMain.fn(an);
          ?}
          }

          主站蜘蛛池模板: 双柏县| 宜兰市| 嘉定区| 潞城市| 宁明县| 克拉玛依市| 南华县| 和田市| 黔西| 广安市| 高安市| 佛学| 高台县| 大英县| 广昌县| 无极县| 泸西县| 巴林左旗| 共和县| 稷山县| 平顶山市| 吉木乃县| 登封市| 永德县| 澳门| 且末县| 梅州市| 芮城县| 潍坊市| 枝江市| 罗江县| 谢通门县| 天祝| 栾城县| 德令哈市| 靖州| 广南县| 兴国县| 巴南区| 诸城市| 焉耆|