返回列表 發帖

繼承 (三)



練習在子類別中添加新的特性,並在子類別中覆寫(override)自父類別繼承下來的方法。
  1. public class Ch70 {

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

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

  32. }

  33. class Dog extends Animal
  34. {
  35.         String color;
  36.         Dog(String n,int a,double w,String c)
  37.         {
  38.                 super(n,a,w);
  39.                 color=c;
  40.         }
  41.        
  42.         void makeSound(int x)
  43.         {
  44.                 for(int i=1; i<=x; i++)
  45.                     System.out.print("汪~");
  46.                 System.out.println();
  47.         }
  48.        
  49.         void showProfile()
  50.         {
  51.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
  52.         }
  53. }

  54. class Cat extends Animal
  55. {
  56.         Cat(String n,int a,double w)
  57.         {
  58.                 super(n,a,w);
  59.         }
  60.        
  61.         void makeSound(int x)
  62.         {
  63.                 for(int i=1; i<=x; i++)
  64.                     System.out.print("喵~");
  65.                 System.out.println();
  66.         }
  67. }
複製代碼

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

  15. class Animal {

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

  31. }

  32. class Cat extends Animal
  33. {
  34.         Cat(String n,int a,double w)
  35.         {
  36.                 super(n,a,w);
  37.         }
  38.       
  39.         void makeSound(int x)
  40.         {
  41.                 for(int i=1; i<=x; i++)
  42.                     System.out.print("喵~");
  43.                 System.out.println();
  44.         }
  45. }
  46. class Dog extends Animal
  47. {
  48.         String color;
  49.         Dog(String n,int a,double w,String c)
  50.         {
  51.                 super(n,a,w);
  52.                 color=c;
  53.         }
  54.       
  55.         void makeSound(int x)
  56.         {
  57.                 for(int i=1; i<=x; i++)
  58.                     System.out.print("汪~");
  59.                 System.out.println();
  60.         }
  61.       
  62.         void showProfile()
  63.         {
  64.                 System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+".");
  65.         }
  66. }
複製代碼

TOP

  1. public class B {

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

  13. }
  14. class Animal {
  15.         String name;
  16.         int age;
  17.         double w;
  18.        
  19.         Animal(String n, int a, double w) {
  20.                 name=n;
  21.                 age=a;
  22.                 this.w=w;
  23.         }
  24.         void showProfile()
  25.         {
  26.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  27.                
  28.         }
  29. }
  30. class Dog extends Animal {
  31.         String color;
  32.         Dog(String n, int a, double w, String c) {
  33.             super(n, a , w);
  34.             color=c;
  35.         }

  36.         void makeSound(int n)
  37.         {
  38.                 for(int i=1; i<=n; i++)
  39.                         System.out.print("汪~");
  40.                 System.out.println();
  41.         }
  42.         void showProfile()
  43.         {
  44.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
  45.                
  46.         }
  47. }
  48. class Cat extends Animal {

  49.         Cat(String n, int a, double w) {
  50.                 super(n, a, w);
  51.         }
  52.         void makeSound(int n)
  53.         {
  54.                 for(int i=1; i<=n; i++)
  55.                         System.out.print("喵~");
  56.                 System.out.println();
  57.         }
  58. }
複製代碼

TOP

  1. public class Ch03 {

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

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

  28. class Dog extends Animal{
  29.         String color;
  30.         Dog(String n,int a,double w ,String c){
  31.                 super(n,a,w);
  32.                 color=c;
  33.         }
  34.         void makeSound(int n)
  35.         {
  36.                 for(int i=1; i<=n; i++)
  37.                         System.out.print("汪~");
  38.                 System.out.println();
  39.         }
  40.         void showProfile()
  41.         {
  42.                 System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  43.         }
  44. }
  45. class Cat extends Animal{
  46.         Cat(String n,int a,double w ){
  47.                 super(n,a,w);
  48.         }
  49.         void makeSound(int n)
  50.         {
  51.                 for(int i=1; i<=n; i++)
  52.                         System.out.print("喵~");
  53.                 System.out.println();
  54.         }

  55. }
複製代碼

TOP

返回列表