返回列表 發帖
  1. public class Ch64 {

  2.         public static void main(String[] args)
  3.         {
  4.         Dog d1=new Dog("憨憨",2,3.8);
  5.         Dog d2=new Dog("球球",1,2.5);
  6.         Cat c1=new Cat("咪咪",5,3.2);
  7.         d1.showprofile();
  8.         d2.showprofile();
  9.         c1.showprofile();
  10.         System.out.println("總共有"+Dog.sum+"隻狗,有"+Cat.sum+"隻貓");
  11.         }
  12. }
  13. class Dog
  14. {
  15.      static int sum=0;
  16.      String name;
  17.      int age;
  18.      double w;
  19.      Dog(String name, int age, double w)
  20.      {
  21.          this.name=name;
  22.          this.age=age;
  23.          this.w=w;
  24.          sum++;
  25.      }
  26.      void showprofile()
  27.      {
  28.              System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
  29.      }
  30. }
  31. class Cat
  32. {
  33.      static int sum=0;
  34.      String name;
  35.      int age;
  36.      double w;
  37.      Cat(String name, int age, double w)
  38.      {
  39.          this.name=name;
  40.          this.age=age;
  41.          this.w=w;
  42.          sum++;
  43.      }
  44.      void showprofile()
  45.      {
  46.              System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
  47.      }
  48. }
複製代碼

TOP

返回列表