返回列表 發帖
  1. public class CH72
  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. }

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

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

TOP

返回列表