1
Eclipse中实现新接口并为现有类生成委托实现的最快方式是什么?如何添加一个带有委托实现的接口到类中?
例如,给定一个现有的类Foo
,假设我希望它使用代表Iterator<Integer>
来实现Iterator<Integer>
。
Eclipse中实现新接口并为现有类生成委托实现的最快方式是什么?如何添加一个带有委托实现的接口到类中?
例如,给定一个现有的类Foo
,假设我希望它使用代表Iterator<Integer>
来实现Iterator<Integer>
。
添加委托场Iterator<Integer>
和implements Iterator<Integer>
到foo
如下:
public class Foo implements Iterator<Integer> {
Iterator<Integer> iterator;
}
选择信号源菜单,然后选择 “生成委托方法”。
检查迭代器框,然后单击确定。结果代码如下所示(取决于您的格式设置)。
public class Foo {
Iterator<Integer> iterator;
public boolean hasNext() { return iterator.hasNext(); }
public Integer next() { return iterator.next(); }
public void remove() { iterator.remove(); }
}