确认此继承有效
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,这是一个
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?如果你想真正扩展
ArrayList
,正确的方法是:你想添加什么样的功能?
If you want to truly extend
ArrayList
, the proper method is:What kind of functionality do you want to add?
您可以通过扩展(如 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 thejava.util.List
interface and delegating the execution of the methods tocustList
(look up Joshua Bloch).您也可以使用委托。
这取决于你想做什么。
然而,使用组合更简单、更好。
You may also use delegation.
It depends what you want to do.
However, using composition is simpler and nicer.