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