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

  2.         public static void main(String[] args) {
  3.                 Dog d1=new Dog("憨憨", 2, 3.8);
  4.                 Dog d2=new Dog("球球", 1, 2.5);
  5.                 Cat c1=new Cat("喵喵", 5, 3.2);
  6.                 d1.showProfile();

  7.                 d2.showProfile();

  8.                 c1.showProfile();

  9.                 System.out.println("有"+Dog.sum+"隻狗"+Cat.sum+"隻貓");
  10.         }      
  11. }

  12. class Dog
  13. {
  14.         static int sum;
  15.         String name;
  16.         int x;
  17.         double y;

  18.         Dog(String n,int x,double d)
  19.         {
  20.                 sum++;
  21.                 name=n;
  22.                
  23.                 this.x=x;
  24.                 this.y=d;
  25.         }

  26.         void showProfile()
  27.         {
  28.                 System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
  29.         }
  30. }
  31. class Cat
  32. {
  33.         static int sum;
  34.         String name;
  35.         int x;
  36.         double y;

  37.         Cat(String n,int x,double d)
  38.         {
  39.                 name=n;
  40.                 sum++;
  41.                 this.x=x;
  42.                 this.y=d;
  43.         }
  44.        
  45.         void showProfile()
  46.         {
  47.                 System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
  48.         }
  49. }
複製代碼
hahahahahahahaha

TOP

返回列表