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