确认此继承有效

发布于 2025-01-04 09:57:32 字数 372 浏览 2 评论 0原文

我正在创建一个 CustomerList 类,当前:

package somestuff;

import java.util.ArrayList;

public class CustomerList {

    ArrayList<Customer> custList;

}

我希望它扩展 ArrayList;这是否是一个有效的“is-a”关系(CustomerList 是一个 ArrayList)?

编辑:抱歉,我没有把这篇文章说得太清楚。我的计划是将其作为 JSP 网站的一部分;我将查询数据库并使用 Customer 和 CustomerList 来存储结果。我的理论是,如果我想改变它们的输出顺序,事情会变得更容易。

I am creating a CustomerList Class, currently:

package somestuff;

import java.util.ArrayList;

public class CustomerList {

    ArrayList<Customer> custList;

}

I want it to extend ArrayList; would this be a valid "is-a" relationship (CustomerList is an ArrayList)?

Edit: Apologies, I haven't made this post too clear. My plan is to have this as part of a JSP website; I'd query a database and use Customer and CustomerList to store the results. My theory is that it will make things easier if I wanted to change the order of how they're outputted.

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

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

发布评论

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

评论(4

是伱的 2025-01-11 09:57:32

不,这是一个has-a 关系。如果您扩展(类)或实现(接口),则 is-a 将是。

在需要时使用 ArrayList不是更简单吗?

No, this is a has-a relationship. is-a will be if you extend (class) or implement (interface).

Wouldn't it be simpler to just use ArrayList<Customer> when needed?

征﹌骨岁月お 2025-01-11 09:57:32

如果你想真正扩展ArrayList,正确的方法是:

public class CustomerList extends ArrayList<Customer> {

    // ... add/override methods here

}

你想添加什么样的功能?

If you want to truly extend ArrayList, the proper method is:

public class CustomerList extends ArrayList<Customer> {

    // ... add/override methods here

}

What kind of functionality do you want to add?

别再吹冷风 2025-01-11 09:57:32

您可以通过扩展(如 Binyamin Sharet 所说)或扩展 java.util.List 接口并委托方法的执行,将其转换为 is-a 关系到 custList(查找 Joshua Bloch)。

You can turn this into an is-a relationship by either extending (as Binyamin Sharet said) or by extending the java.util.List interface and delegating the execution of the methods to custList (look up Joshua Bloch).

咋地 2025-01-11 09:57:32

您也可以使用委托。

class Customer{}
class CustomerList extends AbstractList<Customer>{

    private final List<Customer> customers;
    public CustomerList(List<Customer> customers){
        this.customers = customers;
    }
    @Override
    public Customer get(int index) {
        return customers.get(index);
    }

    @Override
    public int size() {
        return customers.size();
    }

    //Override more methods as needed
}

这取决于你想做什么。
然而,使用组合更简单、更好。

You may also use delegation.

class Customer{}
class CustomerList extends AbstractList<Customer>{

    private final List<Customer> customers;
    public CustomerList(List<Customer> customers){
        this.customers = customers;
    }
    @Override
    public Customer get(int index) {
        return customers.get(index);
    }

    @Override
    public int size() {
        return customers.size();
    }

    //Override more methods as needed
}

It depends what you want to do.
However, using composition is simpler and nicer.

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