[英]Java: Implementation of interfaces and functions that returns interfaces
I'm having a problem with implement interface that have a function which returns a value of a class that implement itself an interface. 我在实现接口上遇到问题,该接口具有一个函数,该函数返回实现自己的接口的类的值。 I have an assignment which said I need to implement those specific interfaces in this way. 我有一个作业,说我需要以这种方式实现那些特定的接口。 These are my interfaces: 这些是我的界面:
public interface Something {
int getValue(); // unique
}
public interface SomethingCollection {
void add(Something something);
Something removeMaxValue();
}
This is the class that implement Something interface: 这是实现Something接口的类:
public class Node implements Something {
private int value;
private Node next;
public Node(int value) {
this.value = value;
this.next = null;
}
public Node(int value, Node next) {
this.value = value;
this.next = next;
}
public int getValue() {
return this.value;
}
public Node getNext() {
return this.next;
}
public void setNext(Node next) {
this.next = next;
}
}
And this is the class that implements SomethingCollection: 这是实现SomethingCollection的类:
public class List implements SomethingCollection {
private Node head;
private int maxValue;
public List() {
this.head = null;
}
public List(Node p) {
Node n = new Node(p.getValue(), p.getNext());
this.head = n;
this.maxValue = this.head.getValue();
}
public void add(Node node) {
if (node.getValue() > this.maxValue) {
this.maxValue = node.getValue();
}
node.setNext(this.head);
this.head = node;
}
public Node removeMaxValue() {
Node current = this.head;
Node prev = this.head;
if (this.head == null) {
return this.head;
}
while (current != null) {
if (current.getValue() == this.maxValue) {
prev.getNext() = current.getNext();
return current;
}
prev = current;
current = current.getNext();
}
}
}
I have in List class this error: "List is not abstract and does not override abstract method add(Something) in SomethingCollection". 我在List类中遇到此错误:“列表不是抽象的,并且不覆盖SomethingCollection中的抽象方法add(Something)”。 I don't know how to fix this problem and what I'm doing wrong. 我不知道如何解决此问题以及我做错了什么。 How can I fix this problem? 我该如何解决这个问题?
Your list does not implement void add(Something something)
. 您的列表没有实现void add(Something something)
。
Sure, it does implement public void add(Node node)
, and a Node is a Something. 当然,它确实实现了public void add(Node node)
,并且Node是Something。 But your class implements an interface. 但是您的类实现了一个接口。 It has to satisfy that interface. 它必须满足该接口。 And that interface includes a method that accepts any type of Something, not just Nodes. 该接口包括一个接受任何类型的Something的方法,而不仅仅是Node。
Here's something else to consider: do you want a Node to be a Something, or to contain a Something? 这里还有其他需要考虑的问题:您是想让一个节点成为某物还是包含某物?
You can use generics in your collection, something like this should work: 您可以在集合中使用泛型,类似这样的方法应该起作用:
interface SomethingCollection <T>
{
public void add(T something);
public T removeMaxValue();
}
class List implements SomethingCollection<Node> {
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.