- public class Ch72 {
- public static void main(String[] args) {
- Dog d1=new Dog("憨憨",2,1.28);
- d1.showProfile();
- Dog d2=new Dog("球球",1,1.35);
- d2.showProfile();
- Cat c1=new Cat("咪咪",3,0.95);
- c1.showProfile();
- }
- }
- 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{
- Dog(String name,int age,Double weight){
- super(name,age,weight);
- }
- }
- class Cat extends Animal{
- Cat(String name,int age,Double weight){
- super(name,age,weight);
- }
- }
複製代碼 |