如何添加一个代理实现的接口到一个类? [英] How can I add an interface with delegate implementations to a class?

查看:136
本文介绍了如何添加一个代理实现的接口到一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如果给定一个现有的类 Foo ,假设我希望它使用委托迭代器<整数> Iterator< Integer> $ c>。

解决方案


  1. 添加委托字段 Iterator< ;整数> 实现Iterator< Integer> foo 如下: p>

      public class Foo implements Iterator< Integer> {
    迭代器<整数>迭代器
    }


  2. 选择源菜单,然后选择生成代理方法。


  3. 检查迭代器框,然后单击确定。所得到的代码将如下所示(取决于您的格式设置)。

      public class Foo {
    Iterator< Integer> ;迭代器

    public boolean hasNext(){return iterator.hasNext(); }
    public Integer next(){return iterator.next(); }
    public void remove(){iterator.remove();
    }



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

这篇关于如何添加一个代理实现的接口到一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆