简体   繁体   中英

How can I add an interface with delegate implementations to a class?

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 the implements Iterator<Integer> to foo as follows:

     public class Foo implements Iterator<Integer> { Iterator<Integer> iterator; } 
  2. Select the source menu and then "Generate Delegate Methods".

  3. 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(); } } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM