返回列表 發帖
本帖最後由 洪振庭 於 2016-7-8 21:02 編輯
  1. public class Ch58{
  2.     public static void main(String args[])
  3.     {
  4.          Dog d1=new Dog("紅棕色","球球",2,2.1);
  5.          Dog d2=new Dog("白色","憨憨",9,3.5);
  6.          d1.showProfile();
  7.          d2.makeSound(5);
  8.          d1.showProfile();
  9.          d2.makeSound(16);
  10.     }
  11. }
  12. class Dog
  13. {
  14.     String color;
  15.     String name;
  16.     int age;
  17.     double w;

  18.     Dog(String c,String n,int a,double w)
  19.     {
  20.         color=c;
  21.         name=n;
  22.         age=a;
  23.         this.w=w;

  24.     }
  25.     void makeSound(int n)
  26.     {
  27.       for(int i=1; i<=n; i++)
  28.         System.out.print("汪~");
  29.       System.out.println();
  30.     }
  31.     void showProfile()
  32.     {
  33.       System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");
  34.     }
  35. }
複製代碼

TOP

返回列表