如何获得“循环”泛型在 Java 中工作吗?

发布于 2024-12-01 05:30:47 字数 1052 浏览 0 评论 0 原文

我在编译涉及一些泛型的以下代码时遇到错误:

public abstract class State<T extends HasAState<? extends State<T>>>{
    protected T parent;

    public void setParent(){ // It's been simplified for the sake of the question!
        parent.removeState(this); // Error here!
        this.parent = parent;
        parent.addState(this);    // Error here!
    }
}

public interface HasAState<T extends State<? extends HasAState<T>>> {
    public void addState(T state);
    public void removeState(T state);
}

错误是: HasAState) ?扩展 State>不适用于参数 (State)

实际上我想要的是这样的:类 A 实现 HasAState类 B 扩展 State< /code> 其中 B 引用了 A 并且可以调用 A.addState(B) (仅因为 B > 延伸State),并且 A 可以调用 B.setParent(this)

我应该如何声明我的类以便我打算做的事情起作用?

谢谢

I am having an error while compiling the following code involving a few generics:

public abstract class State<T extends HasAState<? extends State<T>>>{
    protected T parent;

    public void setParent(){ // It's been simplified for the sake of the question!
        parent.removeState(this); // Error here!
        this.parent = parent;
        parent.addState(this);    // Error here!
    }
}

public interface HasAState<T extends State<? extends HasAState<T>>> {
    public void addState(T state);
    public void removeState(T state);
}

The errors are: The method removeState(capture#1-of ? extends State<T>) in the type HasAState<capture#1-of ? extends State<T>> is not applicable for the arguments (State<T>)

Actually what I would like to have is something like: class A implements HasAState and class B extends State<A> where B has a reference to A and can call A.addState(B) (only because B extends State<A>) and where A can call B.setParent(this).

How should I declare my classes so that what I intend to do work?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剧终人散尽 2024-12-08 05:30:47

我同意埃兰·齐默尔曼的评论。看来你需要重新考虑一下你想要什么。

无论如何,我希望我已经正确理解了问题,所以我将参数从一个更改为两个,以分别描述 statehasAState

public abstract class State<S extends State<S, H>, H extends HasAState<S, H>>{
    protected H parent;

    public void setParent(){ 
        parent.removeState(this);
        this.parent = parent; //!!!this line has no effect!!!
        parent.addState(this);        
    }       
}

public interface HasAState<S extends State<S, H>, H extends HasAState<S, H>> {
    public void addState(State<S, H> state);
    public void removeState(State<S, H> state);
}

这段代码可以编译! -- 请注意 setParent 中第二行的警告。

I agree with the comment of Eran Zimmerman. It seams that you need to rethink about what you want.

Anyway I hope I have understand the problem right, so I changed the Parameter from one to tow, to describe the state and the hasAState separately.

public abstract class State<S extends State<S, H>, H extends HasAState<S, H>>{
    protected H parent;

    public void setParent(){ 
        parent.removeState(this);
        this.parent = parent; //!!!this line has no effect!!!
        parent.addState(this);        
    }       
}

public interface HasAState<S extends State<S, H>, H extends HasAState<S, H>> {
    public void addState(State<S, H> state);
    public void removeState(State<S, H> state);
}

This code compiles! -- be aware of the warning for the second line in setParent.

各自安好 2024-12-08 05:30:47

我的分析 -

  1. A 有一些状态。 (因为 A 实现了 HasState

  2. B 通过 A 具有某种状态(通过包含 A 的引用) ) (
    因为B 扩展了 State


  3. 上面 A 中的状态本质上保存在 B 中(因为 A. addState(B) ) ( ? )

    在我看来,这是一个非常复杂的设计。你能在不提及方法和参数的情况下发布你的设计本质吗?

My Analysis -

  1. A has some state. ( beacuse A implements HasState )

  2. B has some state through A ( by containing a reference of A ) (
    because B extends State<A>)

  3. The state in A above is essentially held in B ( because of A.addState(B) ) ( ? )

    This is a very convoluted design as it appears to me. Can you post what is your design essence without mentioning methods and parameters to them ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文