[英]How to find the concrete class of a method parameter
I have the following class structure: 我具有以下类结构:
public interface Incoming<P extends Processor> {
void doSomething(P p);
}
public interface Processor<I extends Incoming> {
void process(I i);
}
public class MyIncoming implements Incoming<MyProcessor>
{
public void doSomething(MyProcessor p) { .. }
}
public class MyProcessor implements Processor<MyIncoming> {
public void process(MyIncoming i) { .. }
}
Now in another class I pass an Instance of MyIncoming that is supposed to initialize the type passed for the Processor it has defined in doSomething()? 现在在另一个类中,我传递一个MyIncoming实例,该实例应该初始化为其在doSomething()中定义的Processor传递的类型?
Please help. 请帮忙。
The first problem I see with your code is that you are using the raw types Incoming
and Processor
. 我在代码中看到的第一个问题是您使用的是原始类型Incoming
和Processor
。
public interface Incoming<P extends Processor> {
void doSomething(P p); ^^
} that is a raw type!
One way to get rid of those raw types is to make both Incoming
and Processor
have two type parameters, but it's very complicated. 摆脱这些原始类型的一种方法是使Incoming
和Processor
具有两个类型参数,但这非常复杂。
public interface Incoming<I extends Incoming<I, P>, P extends Processor<I, P>> {
void doSomething(P p);
}
public interface Processor<I extends Incoming<I, P>, P extends Processor<I, P>> {
void process(I i);
}
Now to your actual question. 现在是您的实际问题。 You've said that for each concrete implementation of Incoming
you have a specific instance of Processor
, and for a variable t
of type Incoming
, you want to be able to find out that instance x
and call x.process(t);
您已经说过,对于Incoming
每个具体实现,您都有一个特定的Processor
实例,对于Incoming
类型的变量t
,您希望能够找出该实例x
并调用x.process(t);
. 。 I'm sure that this can be done with reflection, but I can't see the point. 我确定可以通过反射来完成,但是我看不到重点。 You can just make getProcessor
a method of Incoming
. 您可以使getProcessor
成为Incoming
的方法。
public interface Incoming<I extends Incoming<I, P>, P extends Processor<I, P>> {
void doSomething(P p);
P getProcessor();
}
Now you can write concrete implementations. 现在,您可以编写具体的实现。
public class MyIncoming implements Incoming<MyIncoming, MyProcessor>
{
private static final MyProcessor PROCESSOR = new MyProcessor();
@Override
public void doSomething(MyProcessor p) { }
@Override
public MyProcessor getProcessor() { return PROCESSOR; }
}
public class MyProcessor implements Processor<MyIncoming, MyProcessor> {
@Override
public void process(MyIncoming i) { }
}
Now, if you have a generic class 现在,如果您有通用类
class A<I extends Incoming<I, P>, P extends Processor<I, P>>
and, within A
, you have a variable i
of type I
, you can do 并且,在A
,您拥有类型I
的变量i
,您可以
i.getProcessor().process(i);
This works, but personally I think circular dependencies of the form TypeA<B extends TypeB>
/ TypeB<A extends TypeA>
are unnecessarily convoluted, and the generics here actually work against you. 这行得通,但我个人认为不必要地使TypeA<B extends TypeB>
/ TypeB<A extends TypeA>
TypeA<B extends TypeB>
循环依赖关系复杂,这里的泛型实际上对您不利。 It may preserve your sanity if you just make Incoming
and Processor
non-generic interfaces and use casting where necessary. 如果您只是将“ Incoming
和“ Processor
非通用接口,并在必要时使用强制转换,则可以保留您的理智。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.