public class SingletonDemo {
public static void main(String[] args) throws Exception {
Singleton service = Singleton.getInstance();
service.doSomething();
}
}
当我创建单例对象时,获得错误 单例无法解析为类型。
你可以这样写
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");
}
}