[英]Interface methods in a class that does not implement it?
public interface Iterator<T> {
// Returns true if the iterator is valid (points to an element), false otherwise.
boolean isValid();
// Returns the current element and moves forward. This method can only be called if the iterator is valid. If the iterator points to the last element, it becomes invalid after the call.
T next();
// Returns the current element and moves backwards. This method can only be called if the iterator is valid. If the iterator points to the first element, it becomes invalid after the call.
T prev();
}
In a class that does not implement interface Iterator, how is it possible to create a method that returns Iterator<K>
, when you can only create methods for an interface inside a class that implements it?在不实现接口 Iterator 的 class 中,当您只能为实现它的 class 中的接口创建方法时,如何创建返回
Iterator<K>
的方法?
public class ABC<K> implements EFG<K>{
public Iterator<K> minIt() {
//method body
//return Iterator<K> variable
}
}
The class ABC
containing the method minIt()
does not implement Iterator<T>
class 包含方法
minIt()
的ABC
未实现Iterator<T>
(No classes implement the interface Iterator <T>
) (没有类实现接口
Iterator <T>
)
Simple.简单的。 By making a class that implements it.
通过制作一个 class 来实现它。 Note that you have a type that you came up with on your own and you named it
Iterator
.请注意,您有一个自己想出的类型,并将其命名为
Iterator
。 Given that java.util.Iterator
exists, this is a really bad idea.鉴于
java.util.Iterator
存在,这是一个非常糟糕的主意。 You should pick another name.你应该选择另一个名字。
public class ABC<K> implements EFG<K> {
// Let's say this contains the items that can be iterated over.
private List<K> list = new ArrayList<K>();
class MyIterator implements my.pkg.Iterator<K> {
private int position = 0;
@Override public boolean isValid() {
return position > -1 && position < list.size();
}
@Override public K next() {
if (!isValid()) throw new NoSuchElementException();
return list.get(position++);
}
@Override public K prev() {
if (!isValid()) throw new NoSuchElementException();
return list.get(position--);
}
}
public Iterator<K> minIt() {
return new MyIterator<K>();
}
}
Note that classes that you put in classes can only be constructed in instance contexts within that class: They have a 'secret' field of your outer's type.请注意,您放入类中的类只能在该 class 内的实例上下文中构造:它们具有外部类型的“秘密”字段。 Hence why the code in MyIterator can access the
list
field of your outer class.因此,为什么 MyIterator 中的代码可以访问外部 class 的
list
字段。
Java has 'anonymous inner class literal' syntax which lets you shorten this: Instead of explicitly declaring class MyIterator
, you can also write: Java 具有“匿名内部 class 字面量”语法,可让您缩短此句法:除了显式声明
class MyIterator
,您还可以编写:
public Iterator<K> minIt() {
return new your.pkg.Iterator<K>() {
private int position = 0;
@Override public boolean isValid() {
// same code goes here as the previous snippet
}
};
}
This anonymous inner class form is a lot more common.这种匿名内部 class 形式更为常见。 It's just syntax sugar - a shorter way to write the same thing.
它只是语法糖——一种编写相同内容的更短方式。
You can use an Anonymous Class that implements the interface:您可以使用实现接口的匿名 Class :
For instance:例如:
interface Foo<T> {
T foo();
}
class Bar<T> {
T t;
public Foo<T> bar() {
return new Foo<T>() { // <-- Anonymous class implementing `Foo`
public T foo() {
return t;
}
};
}
}
Execution:执行:
Bar<String> b = new Bar<>();
b.t = "hello"; // with a setter in real life
Foo<String> f = b.bar();
f.foo(); // will return "hello"
The other option which I think would be the most common is to use a method that returns the interface, for instance the list interface has an iterator()
method even though it itself doesn't implements the Iterator
interface.我认为最常见的另一个选项是使用返回接口的方法,例如列表接口有一个
iterator()
方法,即使它本身没有实现Iterator
接口。
List<String> list = new ArrayList<>();
Iterator<String> stringIterator = list.iterator();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.