【发布时间】:2013-01-28 15:37:49
【问题描述】:
我正在尝试为 System 类实现单例模式。我发现的示例无法编译(例如http://www.tutorialspoint.com/java/java_using_singleton.htm)。非静态类中有一个静态方法。所以我将类设为静态,一切都很好,直到我尝试为我的 Timer 类创建一个成员变量。
现在我收到消息“没有可以访问类型为 scene_3d 的封闭实例。必须使用封闭实例来限定分配...
我已经四处搜索,但没有人为我编译单例模式。顺便说一句,我正在使用处理(Java IDE/扩展)。有关如何解决此问题的任何想法都会有很大帮助。谢谢!
static public class DemoSystem {
private static DemoSystem instance = null;
protected DemoSystem() {}
public static DemoSystem Inst() {
if( instance == null ) {
instance = new DemoSystem();
}
return instance;
}
void init() {
Timer timer = new Timer();
}
int getTime() {
return timer.elapsedTime;
}
}
【问题讨论】:
标签: java singleton static-methods