泛型和访问者模式 [英] Generics and the visitor pattern
问题描述
我对访问者模式和泛型有问题。我有一些抽象类,其子女将被访问。看看这段代码:
public abstract class Element extends SomeSuperClass {
不适用于参数(
public void accept(Visitor< ;? extends Element> v){
v.visit(this);
}
}
public interface Visitor< T extends SomeSuperClass> {
无效访问(T元素);
$ b所以这个想法是:我有一些类层次结构(例如
元素
是SomeSuperClass
的子类。
我有一些通用的访问者
界面来访问这个层次结构。现在在这个层次结构的中间是Element
类,它是抽象的并且有它自己的子类。
现在我想要
Element
来接受其子类的所有访问者,这就是为什么我放这行的原因:public void accept(Visitor< ;? extends Element> v)
但现在我收到错误:
访问方法(<$ c $在类型
Visitor< capture#1-of 中捕获#1-of?extends Element
extends Element>Element
)。
我知道
注意,? extends Element
不是Element
。我的问题是:我可以用不同的方式表达我的想法吗?或者我在这种情况下错过了泛型的概念?T $> c $ c>在
< T extends SomeSuperClass>
中可以是与Element
完全无关的类型,编译器必须确保对于一般情况下访问(T t)
将适用于所有可能的T
。
您调用的代码
Visitor.visit(Element e)
,但访问者可能是Visitor< SubElement> ;
。这是没有道理的。
我认为要求
元素
必须接受其子类的所有访问者没有意义:访问者至少必须能够访问元素
及其所有子类。这将是访客<元素>
。
结构
accept(Visitor< ;? extends Element> v)
表示v
可以是任何这样的Visitor< T>
T扩展Element
。 表示访问者本身的类型是Visitor< ;?扩展Element>
。事实上,Java中甚至不存在这样的事情。每个访问者都有一个与之关联的特定类型参数,而不是通配符。I am having a problem with the Visitor pattern and generics. I have some abstract class whose children are to be visited. Look at this code:
public abstract class Element extends SomeSuperClass { public void accept(Visitor<? extends Element> v) { v.visit(this); } } public interface Visitor<T extends SomeSuperClass> { void visit(T element); }
So the idea is: I have some class hierarchy (e.g.
Element
is a subclass ofSomeSuperClass
). I have got some genericVisitor
interface to visit this hierarchy. Now in the middle of this hierarchy is theElement
class, which is abstract and has it's own subclasses.Now I want
Element
to accept all visitors of its subclasses, which is why I put this line:public void accept(Visitor<? extends Element> v)
But now I am receiving error:
The method visit (
capture#1-of ? extends Element
) in the typeVisitor<capture#1-of ? extends Element>
is not applicable for the arguments (Element
).I understand that
? extends Element
is notElement
. My question is: Can I express my idea in a different way? Or I have just missed the idea of generics in this case?解决方案Note that
T
in<T extends SomeSuperClass>
can be a type completely unrelated toElement
and the compiler must ensure for the general case thatvisit(T t)
will work for every possibleT
.The code you have calls
Visitor.visit(Element e)
, but the visitor in question could beVisitor<SubElement>
. That wouldn't make sense.I think that the requirement "
Element
must accept all visitors of its subclasses" doesn't make sense: the visitor must at least be able to visitElement
and all its subclasses. That would be aVisitor<Element>
.The construct
accept(Visitor<? extends Element> v)
means thatv
can be any suchVisitor<T>
thatT extends Element
. It does not mean that the visitor itself will be of the typeVisitor<? extends Element>
. In fact, no such thing even exists in Java. Every Visitor will have a specific type parameter associated with it, not a wildcard.这篇关于泛型和访问者模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!