public class SingletonDemo {
public static void main(String[] args) throws Exception {
Singleton service = Singleton.getInstance();
service.doSomething();
}
}
当我创建单例对象然后获取错误单例无法解析为类型。
答案 0 :(得分:0)
这就是你可以写它的方式
public class SingletonDemo {
public static void main(String... ignored) {
Singleton service = Singleton.getInstance();
service.doSomething();
}
}
public enum Singleton {
INSTANCE;
public static Singleton getInstance() { return INSTANCE; }
public void doSomething() {
System.out.println("Singleton.doSomething() called");
}
}