- public class ch63
- {
- public static void main(String args[])
- {
- Dog d1=new Dog("憨憨",2,3.8f);
- Dog d2=new Dog("球球",1,2.5f);
- Cat c1=new Cat("咪咪",5,3.2f);
- d1.showProfile();
- d2.showProfile();
- c1.showProfile();
- System.out.println("總共有"+Dog.sum+"隻狗,"+Cat.sum+"隻貓.");
- }
- }
- class Dog
- {
- static int sum=0;
- String name;
- int age;
- float w;
- Dog()
- {
- }
- Dog(String name,int age,float w)
- {
- sum++;
- this.name=name;
- this.age=age;
- this.w=w;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
- }
- }
- class Cat
- {
- static int sum=0;
- String name;
- int age;
- float w;
- Cat()
- {
- }
- Cat(String name,int age,float w)
- {
- sum++;
- this.name=name;
- this.age=age;
- this.w=w;
- }
- void showProfile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤.");
- }
- }
複製代碼 |