- public class Ch01
- {
- public static void main(String[] args)
- {
- Dog a=new Dog("憨憨",2,1.3,"紅棕色");
- Dog b=new Dog("球球",1,1.2,"白色");
- a.showProfile();
- a.makeSound(3);
- System.out.println();
- b.showProfile();
- b.makeSound(2);
- }
- }
- class Dog
- {
- String name, c;
- int age;
- double w;
- Dog(String name, int age, double w, String c)
- {
- this.name=name;
- this.age=age;
- this.w=w;
- this.c=c;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+c);
- }
- void makeSound(int n)
- {
- for(int i=0; i<n;i++)
- System.out.print("汪~");
- }
- }
複製代碼 |