本帖最後由 鄭楀諺 於 2019-9-13 10:26 編輯
- public class Morris {
- 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();
- d2.showProfile();
- c1.showProfile();
- }
- }
- class Animal {
- String name;
- int age;
- double weight;
- Animal(String n, int a, double w)
- {
- name = n;
- age = a;
- weight = w;
- }
- void showProfile(String name, int age, double weight)
- {
- System.out.println(name+"今年"+age+"歲,體重"+"公斤.");
- }
- }
- class Dog extends Animal{
- Dog(String n, int a, double w)
- {
- super(n, a, w);
- }
- }
- class Cat extends Animal{
- Cat(String n, int a, double w)
- {
- super(n, a, w);
- }
- }
複製代碼 |