返回列表 發帖
  1. public class Ch54
  2. {
  3.     public static void main(String[] args)
  4.     {
  5.         Dog d1=new Dog("憨憨",2,1.3,"紅棕色");
  6.         Dog d2=new Dog("球球",1,1.2,"白色");
  7.         d1.showProfile();
  8.         d1.makeSound(2);
  9.         d2.showProfile();
  10.         d2.makeSound(3);
  11.         Cat c1=new Cat("咪咪",3,1.5,"銀灰色");
  12.         c1.ShowproProfile();
  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.     double w;
  23.     Dog(String n, int a, double w, String c)
  24.     {
  25.         sum++;
  26.             name=n;
  27.         age=a;
  28.         this.w=w;
  29.         color=c;
  30.     }
  31.     void showProfile()
  32.     {
  33.         System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+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.         double w;
  48.        
  49.         Cat(String n,int a,double w,String c)
  50.         {
  51.         sum++;
  52.                 name = n;
  53.                 age = a;
  54.                 this.w =w;
  55.                 color=c;
  56.         }
  57.         void ShowproProfile()
  58.         {
  59.             System.out.println(name+"今年"+age+"歲,體重"+w+"公斤,毛色為"+color+".");                       
  60.         }
  61.         void makeSound(int n)
  62.         {
  63.             for(int i=1; i<=n; i++)
  64.             System.out.print("瞄~");
  65.             System.out.println();
  66.             }

  67. }
複製代碼
May

TOP

返回列表