0

如何创建我的实例?

我总是遇到编译错误。

说明 资源路径 位置 类型 不能对非静态类型 COMPONENT ComponentManager.java 进行静态引用

package component;

public class ComponentManager<COMPONENT extends Component> {

private static ComponentManager<COMPONENT> instance = new ComponentManager<COMPONENT >();

private ComponentManager() {

}

public static ComponentManager<?> getInstance() {
    return instance;
}

}
4

2 回答 2

1

您的声明和实例化语法错误​​。像这样创建“实例”:

private static ComponentManager<? extends Component> instance = new ComponentManager< >();
于 2013-09-26T18:18:47.583 回答
0

如果为null,则将new ComponentManager()行放在方法getInstance()调用构造函数中,否则返回。instanceinstance

public class ComponentManager {

    private static ComponentManager instance;

    private ComponentManager() {

    }

    public static ComponentManager getInstance() {
        if (instance  != null) {

        } else {
            instance = new ComponentManager();
        }
        return instance;
    }

}
于 2013-09-26T18:13:00.573 回答