【发布时间】:2020-09-20 13:00:31
【问题描述】:
我了解 ADT(抽象数据类型)隐藏/抽象方法的实现。所以,接口是 ADT,对吗?所以我的问题是:
- 我的示例是否将接口 MyStack 说明为 ADT 及其在类 ImplementationOfMyStack 中的实现,对吗?
- 如果问题 1 是肯定的,那么为什么 Java 库中有一个类 Stack?我的困惑是我可以实例化类 Stack 以使用 push()、pop()、peek(),而无需像我的示例那样编写实现。所以,我认为类 Stack 有它的实现,因此是一个数据结构而不是 ADT。
public interface MyStack {
public void push();
public void pop();
public void peek();
}
public class ImplementationOfMyStack implements MyStack {
public void push() {
System.out.println("Code an implementation here to push a new item on top.");
System.out.println("The implementation is a data structure e.g. linked List.");
}
public void pop() {
System.out.println("Code an implementation here to pop a new item from top.");
System.out.println("The implementation is a data structure e.g. linked List.");
}
public void peek() {
System.out.println("Code an implementation here to peek a new item from top.");
System.out.println("The implementation is a data structure e.g. linked List.");
}
}
【问题讨论】:
-
请关注contribution guidelines如何提出好问题。请不要插入代码的图像。请直接使用编码标签发布您的代码。
-
@flaxel 现在我相应地格式化了我的代码。所以现在值得投票吗? ;)
-
非常好。我没有否决你的问题。
-
附注:接口中的公共说明符是多余的
标签: java adt abstract-data-type