gpt4 book ai didi

java - 如何将一个列表中所做的更改反射(reflect)到另一个列表中?

转载 作者:行者123 更新时间:2023-11-30 06:30:28 25 4
gpt4 key购买 nike

本质上,我想做的是有一个支持列表,它可以获取从其他“子列表”反射(reflect)到它的所有更改。更详细地说,这就是我正在尝试做的事情:

public abstract class User
{

protected List<UserHandler> defaultHandlers
}

public class Customer extends User
{

// All changes made to this list should the one from the parent class
private List<CustomerHandler> userHandlers
}

public class Admin extends User
{

// Similarily with this one
private List<AdminHandler> adminHandlers
}

public interface UserHandler
public interface CustomerHandler extends UserHandler
public interface AdminHandler extends UserHandler

这样的功能可能吗?

最佳答案

显而易见的答案是只使用 defaultHandlersCustomerAdmin ,因为它已经设置为 protected .

但是,如果您正在寻找 defaultHandlers 的子列表这将修改 defaultHandlers ,您可以使用List#subList ;来自其documentation :

List<E> subList(int fromIndex, int toIndex)

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

请记住,如果您的程序是多线程的,则需要处理同步。

关于java - 如何将一个列表中所做的更改反射(reflect)到另一个列表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46257431/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com