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