Java—获得比“更具体的类型”;类别<&燃气轮机&引用;对于参数化泛型类

Java—获得比“更具体的类型”;类别<&燃气轮机&引用;对于参数化泛型类,java,generics,Java,Generics,我有一个基本接口,它使用扩展相同基本接口的类型R进行参数化: public interface IWidget<R extends IWidget<R>> {} 我的问题: 我们可以使用什么更具体的类型来指定MyWidgetManager Class当然有效: public Class<?> ok() { return MyWidgetManager.class; } //然后,可以使用以下命令覆盖默认的get方法: public class Wi

我有一个基本接口,它使用扩展相同基本接口的类型
R
进行参数化:

public interface IWidget<R extends IWidget<R>> {}
我的问题:

我们可以使用什么更具体的类型来指定
MyWidgetManager

Class
当然有效:

public Class<?> ok() {
    return MyWidgetManager.class;
}
//然后,可以使用以下命令覆盖默认的get方法:

public class WidgetB implements IWidget<WidgetB> {}
public class BWidgetManager extends WidgetManagerBase implements IWidgetManager<WidgetB> {}

@Override
public Class<? extends IWidgetManager<?>> getWidgetManagerClass() {
    return BWidgetManager.class;
}
public类WidgetB实现IWidget{}
公共类BWidgetManager扩展WidgetManagerBase实现IWidgetManager{}
@凌驾
公共类>getWidgetManagerClass(){
返回BWidgetManager.class;
}

我真的认为你只是在找

public interface IBaseWidget<R extends IBaseWidget<R>> {}
可以用

public interface IWidgetManager<R extends IWidget<R>, WM extends IWidgetManager<R, WM>> {}

public class MyWidgetManager<R extends IWidget<R>> implements IWidgetManager<R, MyWidgetManager> {}
公共接口IWidgetManager{}
公共类MyWidgetManager实现IWidgetManager{}
因为
MyWidget.class最具体的匹配是
MyWidget.class
test1(){ 返回widget.class; }
公共类为什么不
公共接口IBaseWidget{}
?@EpicPandaForce很好,完成了!谢谢。您是否需要比
更具体的类型?除了以类型安全的方式获取
构造函数
对象和枚举常量(在枚举类型的情况下),使用更具体的
类型做不了多少事情。这只会使你的界面复杂化。我建议坚持使用
,除非真的有必要。@Nándor Előd Fekete问题是这是一个可以重写的方法。我希望其返回类型尽可能具体,以尽可能帮助子类实现者。在使用递归泛型类型定义之前,请确保您确实需要它。如果只要求类型参数是
IWidget
的子类,那么使用
IWidgetI修改接口的名称可以省去很多类型匹配的麻烦,因为我的问题不够清楚。。。最后一个界面(名称
MyWidget
)不是widget本身!我重命名它
IWidgetManager
。不过,我的问题仍然是…关于您的编辑:
WM extensed IWidgetManager
不起作用,因为
IWidgetManager
有两个参数。对,我混淆了这一点,但现在我已经修复了上次出现的
MyWidgetManager
中缺少的
,但事实上,在我可以将其视为
Class
ClassI之后,我事先没有具体的类型,这就是重点。但是
公共类
public <R extends IWidget<?>> Class<? extends IWidgetManager<R>> fails() {
    return MyWidgetManager.class;
}
public <R extends IWidget<R>> Class<? extends IWidgetManager<R>> fails() {
    return MyWidgetManager.class;
}
public Class<? extends IWidgetManager<? extends IWidget<?>>> fails() {
    return MyWidgetManager.class;
}
public interface IWidget<R extends IWidget<?>> {}
public interface IWidgetManager<R extends IWidget<R>> {}
public class WidgetManagerBase {}

// Default implementation
public class WidgetA implements IWidget<WidgetA> {}
public class AWidgetManager extends WidgetManagerBase implements IWidgetManager<WidgetA> {}

// default get method
public Class<? extends IWidgetManager<?>> getWidgetManagerClass() {
    return AWidgetManager.class;
}
public class WidgetB implements IWidget<WidgetB> {}
public class BWidgetManager extends WidgetManagerBase implements IWidgetManager<WidgetB> {}

@Override
public Class<? extends IWidgetManager<?>> getWidgetManagerClass() {
    return BWidgetManager.class;
}
public interface IBaseWidget<R extends IBaseWidget<R>> {}
public interface IWidget<R extends IWidget<R>> extends IBaseWidget<R> {} //not sure about this one.
public class MyWidget implements IWidget<MyWidget> {}
public interface IWidgetManager<R extends IWidget<R>> {}

public class MyWidgetManager<R extends IWidget<R>> implements IWidgetManager<R> {}
public interface IWidgetManager<R extends IWidget<R>, WM extends IWidgetManager<R, WM>> {}

public class MyWidgetManager<R extends IWidget<R>> implements IWidgetManager<R, MyWidgetManager> {}
public Class<? extends IWidget> test() {
    return MyWidget.class;
}
public class ConcreteBaseWidget implements IBaseWidget<ConcreteBaseWidget> {
}

public class ConcreteWidget extends MyWidget<ConcreteBaseWidget> {
}
public Class<? extends IWidget<? extends IBaseWidget<? extends IBaseWidget<?>>>> test1() {
    return ConcreteWidget.class;
}

public Class<? extends IWidget<? extends IBaseWidget<ConcreteBaseWidget>>> test2() {
    return ConcreteWidget.class;
}

public Class<? extends IWidget<? extends ConcreteBaseWidget>> test3() {
    return ConcreteWidget.class;
}