-3
public class SingletonDemo {
    public static void main(String[] args) throws Exception {

        Singleton service = Singleton.getInstance();
        service.doSomething();
    }
}

当我创建单例对象时,获得错误 单例无法解析为类型

4

1 回答 1

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");
    }
}
于 2013-02-02T11:04:27.120 回答