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

心不动则不痛 提交于 2020-01-14 03:14:33

问题


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:


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


来源:https://stackoverflow.com/questions/9500375/how-can-i-add-an-interface-with-delegate-implementations-to-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!