返回列表 發帖
  1. public class Ch61{
  2.     public static void main(String args[])
  3.     {
  4.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  5.         Dog d2=new Dog("球球",1,1.2,"白色");
  6.         d1.showProfile();
  7.         d1.makeSound(2);
  8.         d2.showProfile();
  9.         d2.makeSound(3);
  10.     }
  11. }
  12. class Dog
  13. {
  14.     String name, color;
  15.     int age;
  16.     double w;
  17.     Dog(String n, int a, double w,String c)
  18.     {
  19.         name=n;
  20.         age=a;
  21.         this.w=w;
  22.         color=c;
  23.     }
  24.     void showProfile()
  25.     {
  26.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  27.     }
  28.     void makeSound(int n)
  29.     {
  30.         for(int i=1; i<=n; i++)
  31.             System.out.print("汪~");
  32.         System.out.println();
  33.     }
  34. }
複製代碼

TOP

返回列表