- 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,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 n, int a, Double w){
- name=n;
- age=a;
- weight=w;
- }
-
- void showprofile(){
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤.");
- }
- }
- class Dog extends Animal {
- String color;
- Dog(String n, int a, Double w,String c){
- super(n,a,w);
- color=c;
- }
- void showprofile(){
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+color+",");
- }
- void makesound(int q){
- for (int i = 0; i < q; i++) {
- System.out.print("汪~");
- }
- System.out.println();
- }
- }
- class Cat extends Animal{
- Cat(String n, int a, Double w){
- super(n, a, w);
- }
- void makesound(int q){
- for (int i = 0; i < q; i++) {
- System.out.print("喵~");
- }
- System.out.println();
- }
- }
複製代碼 |