私有接口方法,例如用例? [英] Private interface methods, example use-case?

查看:248
本文介绍了私有接口方法,例如用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为添加对Lambda Expressions的支持的努力的一部分,简要考虑将接口中的私有方法支持包含在Java SE 8中,但是为了更好地专注于Java SE 8的更高优先级任务而被撤回。现在建议支持私有接口方法,从而使接口的非抽象方法能够在它们之间共享代码。

"Support for private methods in interfaces was briefly in consideration for inclusion in Java SE 8 as part of the effort to add support for Lambda Expressions, but was withdrawn to enable better focus on higher priority tasks for Java SE 8. It is now proposed that support for private interface methods be undertaken thereby enabling non abstract methods of an interface to share code between them."

所以说 http://openjdk.java.net/jeps/213 并在错误报告中说 https://bugs.openjdk.java.net/browse/JDK-8071453

So says the specification for http://openjdk.java.net/jeps/213 and says in bug report https://bugs.openjdk.java.net/browse/JDK-8071453 .

但即使是上面给出的简短解释,我也无法想到任何必要的用例。我可以问一个例子,私人接口方法在代码方面是否有用?

But I can't really think of any use-case where this is necessary, even with the short explanation given above. May I ask for an example where "private interface methods" are useful in terms of code?

编辑:所以答案是由于如何在Java 8中为接口添加默认实现,可能存在默认实现使用相同代码库的实例。

So the answer is that due to how default implementations have been added to interfaces in Java 8, there can be instances where the default implementations are using the same codebase.

例如,

public interface MyInterface {
     default void initializeMyClass(MyClass myClass, Params params) {
         //do magical things in 100 lines of code to initialize myClass for example
     }

     default MyClass createMyClass(Params params) {
         MyClass myClass = new MyClass();
         initializeMyClass(myClass, params);
         return myClass;
     }

     default MyClass createMyClass() {
         MyClass myClass = new MyClass();
         initializeMyClass(myClass, null);
         return myClass;
     }
}

愚蠢的例子,我知道。但是,假设我们想在两种方法中使用 initializeMyClass(Params)。但是,如果我们这样做(默认方法),那么 initializeMyClass(Params)将成为公共接口的一部分!为了防止这种情况发生,我们只能在 createMyClass()默认值中保留整个 initializeMyClass(Params)的代码方法。这导致代码重复,这是不可取的。

Silly example, I know. But let's say that we want to use initializeMyClass(Params) in both methods. However, if we do it like this (default method), then initializeMyClass(Params) will be part of the public interface! To prevent that from happening, we can only keep the code of entire initializeMyClass(Params) inside the createMyClass() default methods. Which results in code duplication, which is undesirable.

因此,这会导致重构问题,并且要删除此类代码重复,则允许使用私有默认方法。

Therefore, this causes problem with refactoring, and to remove such code duplication, private default methods are allowed.

感谢您的回答!

推荐答案

接口现在可以默认方法。添加了这些,以便可以在不破坏实现这些已更改接口的所有类的情况下将新方法添加到接口。

Interfaces can now have default methods. These were added so that new methods could be added to interfaces without breaking all classes that implement those changed interfaces.

如果共享代码需要两个默认方法,私有接口方法将允许它们这样做,但不通过公开方式公开该私有方法及其所有实现细节界面。

If two default methods needed to share code, a private interface method would allow them to do so, but without exposing that private method and all its "implementation details" via the interface.

这篇关于私有接口方法,例如用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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