- public class Ch01
- {
-
- public static void main(String[] args) {
- Dog d1=new Dog("憨憨",2,3.8f);
- Dog d2=new Dog("球球",1,2.5f);
- d1.showProfile();
- d2.showProfile();
- Cat c1=new Cat("咪咪",5,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++;
- this.name=n;
- this.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++;
- this.name=n;
- this.age=a;
- this.w=w;
- }
- void showProfile()
- {
- System.out.println(this.name+"今年"+this.age+"歲,體重"+this.w+"公斤.");
- }
- }
複製代碼 |