事情是这样的,刚才写了一下张老师的银行业务调度系统,写了一半
代码如下:
package com.isoftstone.interview.bank;
public enum CustomerType {
Common,Express,Vip
}
package com.isoftstone.interview.bank;
//定义一个NumberMachine用于产生,该类为一单例类
public class NumberMachine {
private NumberManager commonManager=new NumberManager();
private NumberManager expressManager=new NumberManager();
private NumberManager vipManager=new NumberManager();
public NumberManager getCommonManager(){
return commonManager;
}
public NumberManager getExpressManager(){
return expressManager;
}
public NumberManager getVipManager(){
return vipManager;
}
private NumberMachine(){}
private static NumberMachine Instance=new NumberMachine();
public static NumberMachine getInstance(){
return Instance;
}
}
package com.isoftstone.interview.bank;
import java.util.List;
import java.util.ArrayList;
public class NumberManager {
private int lastNumber;
//能用上级接口或父类做的事就不要用子类
private List<Integer> queueNumber=new ArrayList<>();
public synchronized Integer generateNewManager(){
queueNumber.add(lastNumber);
return lastNumber++;
}
public synchronized Integer fetchServiceNumber(){
//取走并返回号码队列中第一个号码
return queueNumber.remove(0);
}
}
package com.isoftstone.interview.bank;
import java.util.concurrent.Executors;
public class ServiceWindow {
private CustomerType type=CustomerType.Common;
public void start(){
Executors.newSingleThreadExecutor().execute(new Runnable(){
public void run(){
while(true){
switch(type){
case Common:
Integer number= NumberMachine.getInstance().getCommonManager().fetchServiceNumber();
break;
case Express:
break;
case Vip:
break;
}
}
}
});
}
问题就是写到一半的时候我引用了NumberMachine类的getInstance方法,但是系统报那是个非静态方法,于是我回去看了是有static啊回来一看依然错,然后我用枚举类创建对象也错,我确认了很多次应该是没错的,可它就报错,这啥情况? |
|