第一次实现java接口,无法编译成功
我需要使用接口java.util.Collection
实现一个包数据结构。 我并不是在数据结构的实际实现方面寻求帮助。我只是无法编译我的程序。我只想在开始实际实现方法之前获得接口的空白实现(带有方法的非功能签名)以进行编译。
class Bag<T> implements java.util.Collection<T>
{
public void Collection () {
}
public boolean add(E e) {
}
public boolean addAll (Collection<? extends E> c) {
}
public void clear() {
}
public boolean contains(Object o) {
}
public boolean containsAll(Collection<?> c) {
}
public boolean equals(Object o) {
}
public int hashCode() {
}
public boolean isEmpty() {
}
public Interator<E> interator() {
}
public boolean remove(Object o) {
}
public boolean removeAll(Collection<?> c) {
}
public int size() {
}
public Object[] toArray() {
}
public <T> T[] toArray(T[] a) {
}
}
编译器在add
等方法的参数中找不到E类。我是否应该为 E
定义一个类,或者我是否不理解 E 的实际含义?编译器说它找不到类 Collection(在 addAll
等方法的参数中)我是否导入 java.util.Collection
还是还有其他我应该知道的事情?编译器也不知道class Iterator
,我也不知道。
我知道这可能都是基本的,但昨天我通过谷歌等找不到任何东西,而且我教授的讲座没有遵循以下项目:全部。我对这个迷失了。感谢您的帮助!
编辑:此外,我还没有对此进行太多搜索,但如果有人可以告诉我有关“?”的任何有用信息,例如 public boolean addAll (Collection c) {}
,我们将不胜感激。
I need to implement a bag data structure using the interface java.util.Collection
.
I'm not asking for help on the actual implementation of the data structure. I just can't get my program to compile. I just want to get a blank implementation of the interface (with non functional signatures of the methods) to compile before I start actually implementing methods.
class Bag<T> implements java.util.Collection<T>
{
public void Collection () {
}
public boolean add(E e) {
}
public boolean addAll (Collection<? extends E> c) {
}
public void clear() {
}
public boolean contains(Object o) {
}
public boolean containsAll(Collection<?> c) {
}
public boolean equals(Object o) {
}
public int hashCode() {
}
public boolean isEmpty() {
}
public Interator<E> interator() {
}
public boolean remove(Object o) {
}
public boolean removeAll(Collection<?> c) {
}
public int size() {
}
public Object[] toArray() {
}
public <T> T[] toArray(T[] a) {
}
}
Compiler can't find class E in the parameters of methods like add
. Am I supposed to define a class for E
, or is there something I'm not understanding about what E actually is? Compiler says it can't find class Collection (in the parameters of methods like addAll
) Do I import java.util.Collection
or is there something else I should know? Compiler also has no idea about class Iterator
and neither do I.
I know this is all probably elementary, but I could not find anything via Google, etc yesterday and my professor's lectures don't follow the projects at all. I'm lost on this one. Thanks for any help!
Edit: Also, I haven't searched as much on this but if someone could tell me anything useful about the "?"s such as public boolean addAll (Collection<? extends E> c) {}
, that would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
T
或E
作为类型参数,然后在整个类定义中一致地使用它。Java 文档使用
E
,因此您可以将第一行更改为:对于那些不返回 void 的方法,您还需要返回值或引发异常。
Either use
T
orE
as the type parameter, and then use it consistently throughout your class definition.The Java documentation uses
E
, so you could for example change the first line to:You will also need to either return values or throw an exception for those methods that don't return void.
E 应该是 T,这是您要存储的通用类型。您的构造函数应该适用于您的类而不是接口。此外,您还需要在具有返回值的方法中至少添加存根返回,例如,如果返回是布尔值,则“返回 false”。
希望这有帮助。
E should be T, which is the generic type you are going to store. Your constructor should be for your class not the interface. Also you will need to add at least stubbed returns in your methods that have a return value, e.g. "return false" if the return is a boolean.
Hope this helps.
尝试将 import 语句更改为
import java.util.*;
Try changing the import statement to
import java.util.*;
因为您没有声明
E
,所以请使用T
代替,并且您还需要返回声明要返回的任何位置。Because you don't have
E
declared , useT
instead and also you need to return wherever it is declared to return.在使用 JAVA 实现接口之前,您必须进行大量基础学习。可能会读“thinking in java”之类的
在您的程序中,存在多个问题:
You are gonna have to do a lot of basic learning before you can implement an interface in JAVA. May be read "thinking in java" or something
In you program there are multiple problems
您的泛型参数称为
T
,并且您在方法的实现中使用E
。如果您使用T
作为元素类型,请更新方法签名。如果至少没有虚拟实现(例如非 void 方法中的
return null;
或return false;
),它可能仍然无法编译。java.util.Iterator 是一个 < href="http://en.wikipedia.org/wiki/Protocol_%28object-oriented_programming%29" rel="nofollow">接口,它描述了一组方法,允许您迭代中的元素你的收藏。您将返回实现该接口及其方法的类的实例。
YourListIterator
可能会保留对列表中您所在位置的引用或索引,返回相应的元素并在列表中向前移动。泛型(包括
?
的使用)在此Your generic parameter is called
T
and you're usingE
in your methods' implementations. If you go withT
as the element type, update the method signatures.It probably still won't compile without at least dummy implementations such as
return null;
orreturn false;
in non-void methods.The java.util.Iterator is an interface that describes a set of methods which allow you to iterate over the elements in your collection. You would return an instance of a class that implements that interface and its methods.
YourListIterator
will probably keep a reference or index to where in the list you are, return the corresponding element and move forward through the list.Generics (including the use of
?
) are discussed in this tutorial.