本帖最後由 劉愷鈞 於 2020-11-13 18:47 編輯
- public class Ch01 {
- public static void main(String[] args) {
- Dog Dog1 =new Dog("憨憨",2,1.3,"紅棕色");
- Dog Dog2=new Dog("球球",1,1.2,"白色");
- Cat Cat1=new Cat("咪咪",3,1.5,"銀灰色");
- Dog1.showProfile();
- Dog1.makeSound(2);
- System.out.println();
- Dog2.showProfile();
- Dog2.makeSound(3);
- System.out.println();
- Cat1.catshowProfile();
- Cat1.catmakeSound(5);
- System.out.println();
- System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓。");
- }
- }
- class Dog
- {
- static int sum=0;
- String name;
- int age;
- double weight;
- String fur;
- Dog(String name,int age,double weight,String fur)
- {
- sum++;
- this.name=name;
- this.age=age;
- this.weight=weight;
- this.fur=fur;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+fur+"。");
- }
- void makeSound(int n)
- {
- for(int a=1;a<=n;a++)
- {
- System.out.print("汪~");
- }
- }
- }
- class Cat
- {
- static int sum=0;
- String name;
- int age;
- double weight;
- String fur;
- Cat(String name,int age,double weight,String fur)
- {
- sum++;
- this.name=name;
- this.age=age;
- this.weight=weight;
- this.fur=fur;
- }
- void catshowProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+weight+"公斤,毛色為"+fur+"。");
- }
- void catmakeSound(int n)
- {
- for(int a=1;a<=n;a++)
- {
- System.out.print("喵~");
- }
- }
- }
複製代碼 |