列表引用List

发布于 2024-11-26 18:18:40 字数 854 浏览 2 评论 0原文

如果我有一个类 A,其中包含类 A 的子级列表,

public Class A
{
    protected List<A> children = new ArrayList<A>();
    ...
}

那么子类 B 是否可以拥有另一个类 C 的子级,并创建对类 A 中的子级列表的引用? A 类具有其他类想要使用的方法,例如将其子级发送到该方法。不过,A类是一般类,B类和C类是比较具体的。因此,为了使代码的阅读更容易并避免大量类型转换,我希望超类的列表引用子类的列表。

因此,当我更新 otherChildren 时,children 列表也会更新。我想做类似下面示例中的操作,但这不起作用,并且也无法转换 (children = (List) otherChildren)

但有办法实现这一目标吗?我真的很想避免所有的类型转换,否则我会得到这样的结果。

public Class B extends A
{
    private List<C> otherChildren = new ArrayList<C>();

    public B()
    {
        children = otherChildren;
        ...
        // Modification of otherChildren will result in the same
        // modification in children
    }
}

public Class C extends A
{
     ...
}

If I have a Class A that contain a List of children with class A like

public Class A
{
    protected List<A> children = new ArrayList<A>();
    ...
}

Is it then possible in a subclass B to have its own children of another class C and create a reference to the children list in class A? Class A has methods that the other classes want to use, e.g. send their children to. However, the class A is the general class, and B and C are more specific. So to make the reading of the code easier and to avoid a lot of typecasting I would like the List of the super class to refer to the List of the subclasses.

So when I update otherChildren, the children list will also be updated. I'd like to do something like in the example below, but does that not work and it can't be casted either (children = (List< A >) otherChildren).

But is there anyway to achieve this? I'd really like to avoid all the typecasting I'll get otherwise.

public Class B extends A
{
    private List<C> otherChildren = new ArrayList<C>();

    public B()
    {
        children = otherChildren;
        ...
        // Modification of otherChildren will result in the same
        // modification in children
    }
}

public Class C extends A
{
     ...
}

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

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

发布评论

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

评论(3

苄①跕圉湢 2024-12-03 18:18:40

你不能这样做,因为它们是不同的类型。您可以对 List 实例执行的操作之一是添加 A 的任意实例;您不能在 List 上执行此操作,因为它是一种更受限制的类型。

解决方法是让 A 接受类型参数:

public class A <T extends A> {
    protected List<T> children = new ArrayList<T>();
    // ...
}

public class B extends A<C> {
    private List<C> otherChildren = new ArrayList<C>();
    public B() {
        children = otherChildren;
    }
    // ...
}

public class C extends A<? extends A> {
    // Or some such
}

当然,此时您也可以说您根本不需要 otherChildren 。您可以直接对子类中的 children 进行操作,并且无需显式强制转换,它仍然可以正常工作。

You can't do that because they're different types. One of the operations you can do on an instance of List<A> is add an arbitrary instance of A; you can't do that on a List<C> because it's a more restricted type.

The fix is to make it so that A takes a type parameter:

public class A <T extends A> {
    protected List<T> children = new ArrayList<T>();
    // ...
}

public class B extends A<C> {
    private List<C> otherChildren = new ArrayList<C>();
    public B() {
        children = otherChildren;
    }
    // ...
}

public class C extends A<? extends A> {
    // Or some such
}

Of course, at that point you can also say that you don't need otherChildren at all. You can operate directly on children in the subclass and it will still all work without explicit casts.

从﹋此江山别 2024-12-03 18:18:40

您所要做的就是将 A 类中子级的声明更改为:

protected List<? extends A> children = new ArrayList<A>();

All you have to do is change the declaration of children in class A to :

protected List<? extends A> children = new ArrayList<A>();
╰沐子 2024-12-03 18:18:40

通过让孩子引用 otherChildren,您试图为 A 提供 List 作为 List 的视图,然后 A 可以合理地期望添加一个它认为一个对象是一个A的列表,但实际上应该是一个C的集合。

这在概念上是无效的。

如果此分配有效,那么在 C 中,您的 otherChildren 可能应该声明为 List。这就引出了一个问题:为什么不能直接使用孩子数组?

编辑:

我认为你需要一个更复杂的 A,也许使用带有“extend”参数的泛型。所以 A 没有 As 列表,而是扩展 As 的事物列表。

By making children refer to otherChildren you are trying to give the A a view of the List<C> as a List<A>, A could then reasonably expect to add an A object to what it thinks is a list of As, but in fact is supposed to be a collection of Cs.

This is conceptually invalid.

If this assignment is valid, then probably in C your otherChildren should be declared as as a List<A>. Which begs the question: why can you not use the children array directly?

edited:

I think you need a more sophisticated A, perhaps using generics with an "extend" parameter. So A doesn't have a list of As but of list of things that extend As.

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