我面临的问题如下-


我使用的是第3方库,例如“编辑器”,它具有接口EditorActions和方法,

create(),edit(),delete()。
我不想在实现中公开EditorActions的方法。所以我的介面会有类似-

myCreate(),myEdit(),myDelete()依次应调用EditorActions方法。

EditorActions只是一个接口,实现在库内部。

如何在不实现两个接口的情况下链接两个接口?


感谢你的帮助

最佳答案

您可以通过公开您希望人们在抽象类中使用的方法来做到这一点。然后强迫人们实施您希望他们使用的特定方法。

然后,您可以使用EditorActions界面中的方法,也可以使用您强制实施的方法。

public abstract class AbstractEditorActions {

   private EditorActions ea;

   public AbstractEditorActions(EditorActions ea) {
      this.ea = ea;
   }

   // In this method, you can use the methods
   // from the interface and from this abstract class.
   // Make the method final so people don't break
   // the implementation.
   public final void yourExposedMethod() {
      // code
      this.toImplement();
      ea.doMethod();
   }

   protected abstract toImplement();

}

关于java - java为另一个接口(interface)创建包装器接口(interface),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/7002080/

10-15 17:09