java Java泛型、扩展泛型和抽象类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/961566/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Generics, extended Generics and abstract classes
提问by Nate W.
I've got the following classes set up:
我已经设置了以下课程:
public abstract class Process<T,S> {
...
}
public abstract class Resource<T, S extends Process<T, S>> {
protected S processer;
...
}
public class ProcessImpl<EventType1, EventType2> {
...
}
public class ResourceImpl extends Resource<EventType1, ProcessImpl> {
processer = new ProcesserImpl();
...
}
Everything is fine until I get to the ResourceImpl. I'm told that ProcessImplis not a valid substitute for the bounded parameter <S extends Process<T,S>>of the type Resource<T,S>.
一切都很好,直到我到达ResourceImpl. 我被告知这ProcessImpl不是<S extends Process<T,S>>type的有界参数的有效替代品Resource<T,S>。
I've tried various ways of getting around this and keep hitting a wall.
我尝试了各种方法来解决这个问题并不断撞墙。
Does anyone have any ideas?
有没有人有任何想法?
回答by Nate W.
public class ProcessImpl<EventType1, EventType2> {
...
}
Because ProcessImpl doesn't extendProcess. Your ProcessImpl is not derived from Process, which is what you're declaring that parameter should be.
因为 ProcessImpl 没有扩展Process。您的 ProcessImpl 不是从 Process 派生的,这就是您声明该参数应该是什么。
回答by Mihai Toader
You might want to do something like this:
你可能想做这样的事情:
public abstract class Process<T, S> {
}
public abstract class Resource<T, S extends Process<T, S>> {
S processor;
}
public class ProcessImpl extends Process<EventType1, ProcessImpl> {
}
public class ResourceImpl extends Resource<EventType1, ProcessImpl> {
}
If you constrain the Sparameter of the Resourceto be a processor you also need to properly declare it on the ProcessImplclass. I don't know what EventType2is but it should be implementing Process interface. I assumed you actually want to say ProcessImpl.
如果将 的S参数限制Resource为处理器,则还需要在ProcessImpl类中正确声明它。我不知道是什么EventType2,但它应该实现 Process 接口。我以为你真的想说ProcessImpl。
回答by Mihai Toader
If you don't want your code to depend on some existing package, which contains the Process, you could also introduce some new interface package depending on nothing in the very bottom of the class hierarchy. (If you are able to change the constrains of the inheritance of course.)
如果您不希望您的代码依赖于某些包含 的现有包,Process您还可以引入一些新的接口包,不依赖于类层次结构最底部的任何内容。(当然,如果您能够更改继承的约束。)
回答by Mihai Toader
I can't see a way to edit the original version, or comment on given answers without a better rep.
我看不到编辑原始版本的方法,或者在没有更好代表的情况下对给定答案发表评论。
This code will exist on a web layer, the eventtype2 is defined on the persistence layer and accessible only in the core layer which exists below this level.
此代码将存在于 Web 层上,事件类型 2 定义在持久层上,并且只能在该层以下的核心层中访问。
So unfortunately without having a tight coupling, which I would like to avoid, I don't have access to EventType2.
所以不幸的是,如果没有我想避免的紧密耦合,我无法访问 EventType2。

