- public class Ch50 {
- public static void main(String[] args) {
- Dog d1=new Dog("憨憨", 2, 3.8);
- Dog d2=new Dog("球球", 1, 2.5);
- Cat c1=new Cat("喵喵", 5, 3.2);
- d1.showProfile();
- d2.showProfile();
- c1.showProfile();
- System.out.println("有"+Dog.sum+"隻狗"+Cat.sum+"隻貓");
- }
- }
- class Dog
- {
- static int sum;
- String name;
- int x;
- double y;
- Dog(String n,int x,double d)
- {
- sum++;
- name=n;
-
- this.x=x;
- this.y=d;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
- }
- }
- class Cat
- {
- static int sum;
- String name;
- int x;
- double y;
- Cat(String n,int x,double d)
- {
- name=n;
- sum++;
- this.x=x;
- this.y=d;
- }
-
- void showProfile()
- {
- System.out.println(name+"今年"+x+"歲,體重"+y+"公斤");
- }
- }
複製代碼 |