如何创建一个实现java.util.collections的类 [英] How to create a class that implements java.util.collections
问题描述
我试图创建一个类, MyStack
将实现一个java.util.collections类。 MyStack
将覆盖集合类的一些方法,如add(类似于push),remove(类似于pop)等。我打算在类a 设置
或集合类的其他接口,除了 MyStack
不会是一个接口或抽象类,创建 MyStack
类型的对象。
I am trying to create a class say MyStack
that would implement a java.util.collections class. MyStack
will override some methods of the collections class like add(similar to push), remove(similar to pop) etc.. I intend to model the class on the same lines as a Set
or other interfaces of the collection class except that MyStack
would not be an interface or abstract class and that we can create objects of type MyStack
.
我有语法的问题,因为我不知道我是否正确的方向。我到目前为止都是这样的 - 注意 - 没有方法已经被定义到目前为止 - 我试图得到的框架权利,然后继续定义的方法。
I have problems with the syntax as I am not sure if I am proceeding in the right direction.All I have so far is something like this - NOTE - None of the methods have been defined so far - I am trying to get teh skeleton right before proceeding to define the methods.
import java.util.*;
public class MyStak implements java.util.Collection<E>{
public boolean add(E o){
return false;
}
public boolean addAll(Collection c){
return false;
}
public void clear() {
}
public boolean contains(Object o){
return false;
}
public boolean containsAll(Collection o){
return false;
}
public boolean equals(Object c){
return false;
}
public int hashcode(){
return 0;
}
public boolean isEmpty(){
return false;
}
public Iterator iterator(){
return null;
}
public boolean remove(Object o){
return false;
}
public boolean removeAll(Collection o){
return false;
}
public boolean retainAll(Collection o){
return false;
}
public int size(){
return 1;
}
public Object[] toArray(){
return null;
}
public Object[] toArray(Object[] a){
return null;
}
}
我有几个编译时间错误像 -
I have a couple of compile time errors like -
+public class MyStak implements java.util.Collection<E>{
Multiple markers at this line
- The type MyStak must implement the inherited abstract method
Collection<E>.add(E)
- E cannot be resolved to a type
+public boolean add(E o){
Multiple markers at this line
- E cannot be resolved to a type
- implements
java.util.Collection<E>.add
我的代码修改,示例,我的代码更正,教程链接等,
Any code modifications, examples , corrections to my code , links to tutorials etc will we highly appreciated.
推荐答案
确保在< E>
您的班级:
public class MyStak<E> implements java.util.Collection<E>
^^^
如果你想让自己的生活更容易尝试子分类< a href =http://download.oracle.com/javase/6/docs/api/java/util/AbstractCollection.html> AbstractCollection
直接实现 Collection
。它为大多数方法提供了合理的默认实现,以最小化需要编写的代码量。
If you want to make life easier on yourself try sub-classing AbstractCollection
instead of implementing Collection
directly. It provides reasonable default implementations for most of the methods to minimize the amount of code you need to write.
java.util
AbstractCollection< E>
此类提供了
Collection
接口,以尽量减少实现此接口所需的工作量。
java.util
Class
AbstractCollection<E>
This class provides a skeletal implementation of the
Collection
interface, to minimize the effort required to implement this interface.
要实现一个不可修改的集合,程序员只需要扩展这个类并提供迭代器
和 size
方法的实现。 ( iterator
方法返回的迭代器必须实现 hasNext
和 next
。)
To implement an unmodifiable collection, the programmer needs only to extend this class and provide implementations for the iterator
and size
methods. (The iterator returned by the iterator
method must implement hasNext
and next
.)
要实现可修改的集合,程序员必须另外覆盖此类的 add
方法抛出 UnsupportedOperationException
),并且 iterator
方法返回的迭代器必须另外实现其 remove 方法。
To implement a modifiable collection, the programmer must additionally override this class's add
method (which otherwise throws an UnsupportedOperationException
), and the iterator returned by the iterator
method must additionally implement its remove
method.
程序员通常应提供 void
code> Collection 接口规范中的建议。
The programmer should generally provide a void
(no argument) and Collection
constructor, as per the recommendation in the Collection
interface specification.
这篇关于如何创建一个实现java.util.collections的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!