返回列表 發帖

繼承 (二)

本帖最後由 tonyh 於 2015-4-15 14:56 編輯



範例程式碼中, 第40~45行與54~59行, 為子類別中新添加的特性.
  1. public class ch67
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.28);
  6.         d1.showProfile();
  7.         d1.makeSound(2);
  8.         Dog d2=new Dog("球球",1,1.35);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.         Cat c1=new Cat("咪咪",3,0.95);
  12.         c1.showProfile();
  13.         c1.makeSound(5);
  14.     }
  15. }

  16. class Animal
  17. {
  18.     String name;
  19.     int age;
  20.     double w;
  21.     Animal(String name, int age, double w)
  22.     {
  23.         this.name=name;
  24.         this.age=age;
  25.         this.w=w;
  26.     }
  27.     void showProfile()
  28.     {
  29.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  30.     }
  31. }

  32. class Dog extends Animal
  33. {
  34.     Dog(String name, int age, double w)
  35.     {
  36.         super(name, age, w);
  37.     }
  38.     void makeSound(int x)
  39.     {
  40.         for(int i=0; i<x; i++)
  41.             System.out.print("汪~");
  42.         System.out.println();
  43.     }
  44. }

  45. class Cat extends Animal
  46. {
  47.     Cat(String name, int age, double w)
  48.     {
  49.         super(name, age, w);
  50.     }
  51.     void makeSound(int x)
  52.     {
  53.         for(int i=0; i<x; i++)
  54.             System.out.print("喵~");
  55.         System.out.println();
  56.     }
  57. }
複製代碼
附件: 您需要登錄才可以下載或查看附件。沒有帳號?註冊

  1. public class Ch67
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                  Dog d1=new Dog("憨憨",2,1.28);
  6.              d1.showprofile();
  7.              d1.makeSound(2);
  8.              Dog d2=new Dog("球球",1,1.35);
  9.              d2.showprofile();
  10.              d2.makeSound(3);
  11.              Cat c1=new Cat("喵喵",3,0.95);
  12.              c1.showprofile();
  13.              c1.makeSound(5);
  14.         }
  15. }
  16. class Animal
  17. {
  18.     String name;
  19.     int age;
  20.     double w;
  21.     Animal(String name, int age, int w)
  22.     {
  23.             this.name=name;
  24.             this.age=age;
  25.             this.w=w;
  26.     }
  27.     void showprofile()
  28.     {
  29.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
  30.     }
  31. }
  32. class Dog extends Animal
  33. {
  34.     Dog(String name, int age, int w)
  35.     {
  36.             super(name,age,w);
  37.     }
  38.     void makeSound(int x)
  39.     {
  40.             for(int i=0; i<x; i++)
  41.                     System.out.println("汪~~");
  42.             System.out.println();
  43.     }
  44. }
  45. class Cat extends Animal
  46. {
  47.     Cat(String name, int age, int w)
  48.     {
  49.             super(name,age,w);
  50.     }
  51.     void makeSound(int x)
  52.     {
  53.             for(int i=0; i<x; i++)
  54.                     System.out.println("喵~~");
  55.             System.out.println();
  56.     }
  57. }
複製代碼

TOP

  1. public class ch67
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.28);
  6.         d1.showProfile();
  7.         d1.makeSound(2);
  8.         Dog d2=new Dog("球球",1,1.35);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.         Cat c1=new Cat("咪咪",3,0.95);
  12.         c1.showProfile();
  13.         c1.makeSound(5);
  14.     }
  15. }

  16. class Animal
  17. {
  18.     String name;
  19.     int age;
  20.     double w;
  21.     Animal(String name, int age, double w)
  22.     {
  23.         this.name=name;
  24.         this.age=age;
  25.         this.w=w;
  26.     }
  27.     void showProfile()
  28.     {
  29.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  30.     }
  31. }

  32. class Dog extends Animal
  33. {
  34.     Dog(String name, int age, double w)
  35.     {
  36.         super(name, age, w);
  37.     }
  38.     void makeSound(int x)
  39.     {
  40.         for(int i=0; i<x; i++)
  41.             System.out.print("汪~");
  42.         System.out.println();
  43.     }
  44. }

  45. class Cat extends Animal
  46. {
  47.     Cat(String name, int age, double w)
  48.     {
  49.         super(name, age, w);
  50.     }
  51.     void makeSound(int x)
  52.     {
  53.         for(int i=0; i<x; i++)
  54.             System.out.print("喵~");
  55.         System.out.println();
  56.     }
  57. }
複製代碼

TOP

  1. public class ch67
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.28);
  6.         d1.showProfile();
  7.         d1.makeSound(2);
  8.         Dog d2=new Dog("球球",1,1.35);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.         Cat c1=new Cat("咪咪",3,0.95);
  12.         c1.showProfile();
  13.         c1.makeSound(5);
  14.     }
  15. }

  16. class Animal
  17. {
  18.     String name;
  19.     int age;
  20.     double w;
  21.     Animal(String name, int age, double w)
  22.     {
  23.         this.name=name;
  24.         this.age=age;
  25.         this.w=w;
  26.     }
  27.     void showProfile()
  28.     {
  29.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  30.     }
  31. }

  32. class Dog extends Animal
  33. {
  34.     Dog(String name, int age, double w)
  35.     {
  36.         super(name, age, w);
  37.     }
  38.     void makeSound(int x)
  39.     {
  40.         for(int i=0; i<x; i++)
  41.             System.out.print("汪~");
  42.         System.out.println();
  43.     }
  44. }

  45. class Cat extends Animal
  46. {
  47.     Cat(String name, int age, double w)
  48.     {
  49.         super(name, age, w);
  50.     }
  51.     void makeSound(int x)
  52.     {
  53.         for(int i=0; i<x; i++)
  54.             System.out.print("喵~");
  55.         System.out.println();
  56.     }
  57. }
複製代碼

TOP

  1. public class ch67
  2. {
  3.     public static void main(String args[])
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.28);
  6.         d1.showProfile();
  7.          d1.makeSound(2);
  8.         Dog d2=new Dog("球球",1,1.35);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.         Cat c1=new Cat("咪咪",3,0.95);
  12.         c1.showProfile();
  13.         c1.makeSound(5);
  14.     }
  15. }

  16. class Animal
  17. {
  18.     String name;
  19.     int age;
  20.     double w;
  21.     Animal(String name, int age, double w)
  22.     {
  23.         this.name=name;
  24.         this.age=age;
  25.         this.w=w;
  26.     }
  27.     void showProfile()
  28.     {
  29.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  30.     }
  31. }

  32. class Dog extends Animal
  33. {
  34.     Dog(String name, int age, double w)
  35.     {
  36.         super(name, age, w);
  37.     }
  38.     void makeSound(int x)
  39.     {
  40.         for(int i=0; i<x; i++)
  41.             System.out.print("汪~");
  42.         System.out.println();
  43.     }
  44. }

  45. class Cat extends Animal
  46. {
  47.     Cat(String name, int age, double w)
  48.     {
  49.         super(name, age, w);
  50.     }
  51.     void makeSound(int x)
  52.     {
  53.         for(int i=0; i<x; i++)
  54.             System.out.print("喵~");
  55.         System.out.println();
  56.     }
  57. }
複製代碼

TOP

返回列表