public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建一个羊类,使用static实现数羊,记录一共创建了几只羊。
Sheep.getCount();
Sheep s1=new Sheep("喜羊羊");
Sheep.getCount();
Sheep s2=new Sheep("懒羊羊");
Sheep.getCount();
Sheep s3=new Sheep("美羊羊");
Sheep.getCount();
}
}
public class Sheep {
private String name;
private static int count=0;//计数器
public Sheep(String name){
this.name=name;
count++;
}
//显示羊的数量
public static void getCount(){
System.out.println("现在羊的数量是:"+count);
}
}
转载于:https://blog.51cto.com/12170190/1876195