问题
What is the fastest way in Eclipse to implement a new interface and generate delegate implementations to an existing class?
For instance given an existing class Foo
, suppose I want it to implement Iterator<Integer>
using a delegate Iterator<Integer>
.
回答1:
Add the delegate field
Iterator<Integer>
and theimplements Iterator<Integer>
tofoo
as follows:public class Foo implements Iterator<Integer> { Iterator<Integer> iterator; }
Select the source menu and then "Generate Delegate Methods".
Check the iterator box and click OK. The resulting code will look as follows (depending on your formatting settings).
public class Foo { Iterator<Integer> iterator; public boolean hasNext() { return iterator.hasNext(); } public Integer next() { return iterator.next(); } public void remove() { iterator.remove(); } }
来源:https://stackoverflow.com/questions/9500375/how-can-i-add-an-interface-with-delegate-implementations-to-a-class