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