返回列表 發帖
  1. public class Ch01
  2. {
  3.         public static void main(String[] args)
  4.         {
  5.                 Dog d1=new Dog("憨憨",2,1.3f,"紅棕色");
  6.                 Dog d2=new Dog("球球",1,1.2f,"白色");
  7.                 Cat c1=new Cat("咪咪",3,1.5f,"銀灰色");
  8.                 d1.showProfile();
  9.                 d1.makeSound(2);
  10.                 d2.showProfile();
  11.                 d2.makeSound(3);
  12.                 c1.showProfile();
  13.                 c1.makeSound(5);
  14.                 System.out.println("總共有"+Dog.sum+"隻狗, "+Cat.sum+"隻貓");

  15.         }
  16. }
  17. class Dog
  18. {
  19.     static int sum=0;
  20.         String name,color;
  21.     int age;
  22.     float weight;
  23.     Dog(String n,int a,float w,String c)
  24.     {
  25.             name=n;
  26.             age=a;
  27.             weight=w;
  28.             color=c;
  29.             sum++;
  30.     }
  31.     void showProfile()
  32.     {
  33.             System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤, 毛色為"+color+".");
  34.     }
  35.     void makeSound(int n)
  36.     {
  37.             for(int i=1;i<=n;i++)
  38.                     System.out.print("汪~");
  39.             System.out.println();
  40.     }
  41. }
  42. class Cat
  43. {
  44.         static int sum=0;
  45.         String name,color;
  46.         int age;
  47.         float weight;
  48.         Cat(String n,int a,float w,String c)
  49.         {
  50.                 name=c;
  51.                 age=a;
  52.                 weight=w;
  53.                 color=c;
  54.                 sum++;
  55.         }
  56.         void showProfile()
  57.         {
  58.                 System.out.println(name+"今年"+age+"歲, 體重"+weight+"公斤, 毛色為"+color+".");
  59.         }
  60.         void makeSound(int n)
  61.         {
  62.                 for(int i=1;i<=n;i++)
  63.                 {
  64.                         System.out.print("喵~");
  65.                 }
  66.                 System.out.println();
  67.         }
  68.        
  69. }
複製代碼

TOP

返回列表