- public class Ch01 {
- 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,1.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(){
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
- }
- }
- 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);
- }
- }
複製代碼 回復 1# tonyh |