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