泛型和访问者模式 [英] Generics and the visitor pattern

查看:120
本文介绍了泛型和访问者模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对访问者模式和泛型有问题。我有一些抽象类,其子女将被访问。看看这段代码:

  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 of SomeSuperClass). I have got some generic Visitor interface to visit this hierarchy. Now in the middle of this hierarchy is the Element 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 type Visitor<capture#1-of ? extends Element> is not applicable for the arguments (Element).

I understand that ? extends Element is not Element. 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 to Element and the compiler must ensure for the general case that visit(T t) will work for every possible T.

The code you have calls Visitor.visit(Element e), but the visitor in question could be Visitor<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 visit Element and all its subclasses. That would be a Visitor<Element>.

The construct accept(Visitor<? extends Element> v) means that v can be any such Visitor<T> that T extends Element. It does not mean that the visitor itself will be of the type Visitor<? 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆