返回列表 發帖
本帖最後由 劉愷鈞 於 2020-11-13 18:47 編輯
  1. public class Ch01 {
  2.         public static void main(String[] args) {
  3.                 Dog Dog1 =new Dog("憨憨",2,1.3,"紅棕色");
  4.                 Dog Dog2=new Dog("球球",1,1.2,"白色");
  5.                 Cat Cat1=new Cat("咪咪",3,1.5,"銀灰色");
  6.                 Dog1.showProfile();
  7.                 Dog1.makeSound(2);
  8.                 System.out.println();
  9.                 Dog2.showProfile();
  10.                 Dog2.makeSound(3);
  11.                 System.out.println();
  12.                 Cat1.catshowProfile();
  13.                 Cat1.catmakeSound(5);
  14.                 System.out.println();
  15.                 System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓。");

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

TOP

返回列表