- public class Ch33 {
- public static void main(String[] args) {
- Dog d1=new Dog("史東西",2,1.28,"灰色");
- Dog d2=new Dog("使冬吸",1,1.35,"黑色");
- Cat c1=new Cat("哈哈",3,0.95,"白色");
- d1=showProfile();
- d1=makeSound(2);
- d2.showProfile();
- d2=makeSound(3);
- c1.showProfile();
- c1=makeSound(5);
- }
- class Animal
- {
- String name;
- int age;
- double weigth;
-
- Animal(String n,int a,double w)
- {
- this.name=n;
- this.age=a;
- this.weigth=w;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲"+"體重"+weigth+"kg"+"毛色為");
- }
- }
- class Dog extends Animal
- {
- String color;
- Dog(String n, int a,double w,String c)
- {
- super(n,a,w);
- color=c;
- }
- void makeSound(int x)
- {
- for(int i=1;i<=x;i++)
- System.out.println("尢");
- System.out.println();
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲"+"體重"+weigth+"kg"+"毛色為");
- }
- }
- class Cat extends Animal
- {
- Cat(String n,int a,double w)
- {
- super(n,a,w);
- }
- void makeSound(int x)
- {
- for(int i=1;i<=x;i++)
- System.out.println("ㄇㄧㄠ");
- System.out.println();
- }
- }
- }
複製代碼 |