在我的编码中,我使用Singleton
带有Singleton Design Pattern
. 问题是为什么它的子类不允许使用默认构造函数?
我得到编译时错误:
Implicit super constructor Singleton() is not visible. Must explicitly invoke another constructor
单例.java
public class Singleton {
private static Singleton singleton;
private Singleton() {
System.out.println("I am user class");
}
public static Singleton getInstance() {
if(singleton == null) {
singleton = new Singleton();
}
return singleton;
}
}
子类.java
public class SubClass extends Singleton {
public SubClass(){
System.out.println("I am sub class");
}
}