- public class Ch64 {
- 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=0;
- String name;
- int age;
- double w;
- Dog(String name, int age, double w)
- {
- this.name=name;
- this.age=age;
- this.w=w;
- sum++;
- }
- void showprofile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
- }
- }
- class Cat
- {
- static int sum=0;
- String name;
- int age;
- double w;
- Cat(String name, int age, double w)
- {
- this.name=name;
- this.age=age;
- this.w=w;
- sum++;
- }
- void showprofile()
- {
- System.out.println(name+"今年"+age+"歲,體重"+w+"公斤");
- }
- }
複製代碼 |