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