返回列表 發帖
  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

返回列表