java继承与组合(实现堆栈) [英] java inheritance versus composition (implementing a stack)

查看:127
本文介绍了java继承与组合(实现堆栈)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在java中实现一个堆栈(使用列表界面:接口列表)。

I am trying to implement a Stack in java (using the list interface: Interface List).

我想用两种不同的方式实现它:使用组合和继承。

I want to implement it two different ways: using composition and inheritance.

对于继承,到目前为止我有:

For inheritance, so far I have:

 import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 import java.util.ListIterator;

 public class StackInheritance implements List {
      //implement list methods
 }

对于作文,我有:

 import java.util.List;

 public abstract class StackComposition implements List {
     // implement some standard methods
 }

 public class StackViaList extends StackComposition {
     // implement methods that have not been implemented in the abstract
     // class
 }

我很困惑从哪里开始。我之前从未使用过接口,所以我是否应该使用List方法模仿堆栈,例如使用Array或ArrayList?

I am confused as to where to go from here. I have never used an interface before, so am I supposed to use the List methods to "mimic" a stack, using an Array or an ArrayList for example?

此外,对于组合,我不明白应该进入StackComposition的方法以及应该进入StackViaList的方法。在不完全理解接口以及继承和组合之间,我有点迷失。我似乎还不能得到它...

Also, for composition, I don't understand what methods should go into StackComposition and what should go into StackViaList. Between not fully understanding interfaces as well as inheritance and composition, I'm a bit lost. I can't seem to just "get it" yet...

任何帮助都将不胜感激,谢谢!

Any help would be appreciated, thanks!

推荐答案

对于组合,堆栈类应该有一个列表,而不是实现或扩展基于列表的类。继承是一种IS A关系,而组合是HAS A关系。

For composition, the stack class should HAVE a list, not implement or extend a List-based class. Inheritance is an "IS A" relationship, whereas composition is a "HAS A" relationship.

例如:

public class StackWithComposition
{
    // StackWithComposition HAS A List (rather than IS A List)
    private List myList = new ArrayList();

    public void push(object item)
    {
        // add item to myList, etc.
    }

    public object pop()
    {
        // return item at top (or end) of myList
    }

    // etc.
}

请注意,您可能希望将其设为泛型类,而不是处理原始对象,但这可能是个主意。

Note that you would probably want to make this a generic class, rather than dealing with raw objects, but this would be the idea.

在这种情况下,基于组合的解决方案可能优于基于继承的解决方案。当你从类/接口继承时,你应该问自己,Stack是List吗?大多数Stacks不应该为用户提供对所有原始List方法的访问权限,因此最好隐藏您正在使用List作为内部数据结构的面。使用组合列表可以完全隐藏您使用List作为内部结构的事实。

In this case, the composition-based solution is probably preferable over the inheritance-based solution. When you inherit from a class/interface, you should ask yourself, is the Stack a List? Most Stacks should not provide the user with access to all the raw List methods, so it's better to hide the face that you're using a List as the internal data structure. Using a composed List allows you to completely hide the fact that you're using a List as the internal structure.

这篇关于java继承与组合(实现堆栈)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆