- public class Ch52
- {
- public static void main(String[] args)
- {
- Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
- Dog d2=new Dog("球球",1,1.2,"白色");
- Cat c1=new Cat("咪咪",3,1.5,"銀灰色");
- d1.showProfile();
- d1.makeSound(2);
- d2.showProfile();
- d2.makeSound(3);
- c1.showProfile();
- c1.makeSound(5);
- System.out.println("總共有 "+Dog.sum+"隻狗, "+Cat.sum+"隻貓");
- d1.makeSound(1);
- d2.makeSound(1);
- c1.makeSound(1);
- }
- }
- class Dog
- {
- String name, color;
- int age;
- double w;
- static int sum=0;
- Dog(String n, int a, double w, String c)
- {
- name=n;
- age=a;
- this.w=w;
- color=c;
- sum++;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
- }
- void makeSound(int n)
- {
- for(int i=1; i<=n; i++)
- System.out.print("汪~");
- System.out.println();
- }
- }
- class Cat
- {
- String name, color;
- int age;
- double w;
- static int sum=0;
- Cat(String n, int a, double w, String c)
- {
- name=n;
- age=a;
- this.w=w;
- color=c;
- sum++;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
- }
- void makeSound(int n)
- {
- for(int i=1; i<=n; i++)
- System.out.print("喵~");
- System.out.println();
- }
- }
複製代碼 |