- public class Ch70 {
- 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 weight;
-
- Animal(String name, int age, double weight) {
- this.name = name;
- this.age = age;
- this.weight = weight;
- }
-
- void showProfile() {
- System.out.println(name + "今年" + age + "歲,體重" + weight + "公斤.");
- }
- }
- class Dog extends Animal {
- String color;
-
- Dog(String name, int age, double weight, String color) {
- super(name, age, weight);
- this.color = color;
- }
-
- void makeSound(int x) {
- for(int i=1; i<=x; i++)
- System.out.print("汪~");
- System.out.println();
- }
-
- void showProfile() {
- System.out.println(name + "今年" + age + "歲,體重" + weight + "公斤,毛色為" + color + ".");
- }
- }
- class Cat extends Animal {
- String color;
-
- Cat(String name, int age, double weight) {
- super(name, age, weight);
- }
-
- void makeSound(int x) {
- for(int i=1; i<=x; i++)
- System.out.print("喵~");
- System.out.println();
- }
- }
複製代碼 |