//Voter类
package cn.com.ooa3.staticdemo;
//模拟选民投票,每人一票,100截止
public class Voter {
//目前投票数,被所有选民实例所共享
static int count;
//投票总数最大值,到100时,就停止投票
static final int MAX_COUNT=100;
//选民名字
private String name;
public Voter(){}
public Voter(String name){
this.name=name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//选民投票
public void vote(){
if(count==100){
System.out.println("投票数已达到100,投票已停止!");
return;
}else{
count++;
System.out.println(this.name+"选民投票成功!当前投票总数为:"+count);
}
}
}
//TestVoter类
package cn.com.ooa3.staticdemo;
public class TestVoter {
public static void main(String[] args) {
Voter v1=new Voter("张三");
v1.vote();
Voter v2=new Voter("李四");
v2.vote();
Voter v3=new Voter("王五");
v3.vote();
for (int i=0;i<=97;i++){
Voter v=new Voter("v"+(i+1));
v.vote();
}
}
}