java 私有接口方法,示例用例?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29040821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Private interface methods, example use-case?
提问by EpicPandaForce
"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."
“对接口中私有方法的支持曾被简要考虑过包含在 Java SE 8 中,作为添加对 Lambda 表达式支持的努力的一部分,但被撤回以更好地关注 Java SE 8 的更高优先级任务。现在建议支持私有接口方法,从而使接口的非抽象方法能够在它们之间共享代码。”
So says the specification for http://openjdk.java.net/jeps/213and says in bug report https://bugs.openjdk.java.net/browse/JDK-8071453.
所以说http://openjdk.java.net/jeps/213的规范, 并在错误报告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?
但我真的想不出任何需要这样做的用例,即使上面给出了简短的解释。我可以问一个例子,“私有接口方法”在代码方面很有用吗?
EDIT: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.
编辑:所以答案是,由于默认实现是如何添加到 Java 8 中的接口的,因此可能存在默认实现使用相同代码库的情况。
For example,
例如,
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;
}
}
Silly example, I know. But let's say that we want to use initializeMyClass(MyClass, Params)
in both methods. However, if we do it like this (default method), then initializeMyClass(MyClass, Params)
will be part of the public interface! To prevent that from happening, we can only keep the code of entire initializeMyClass(MyClass, Params)
inside the createMyClass()
default methods. Which results in code duplication, which is undesirable.
愚蠢的例子,我知道。但是假设我们想initializeMyClass(MyClass, Params)
在这两种方法中使用。但是,如果我们这样做(默认方法),那么initializeMyClass(MyClass, Params)
将成为公共接口的一部分!为了防止这种情况发生,我们只能将整个代码保留initializeMyClass(MyClass, Params)
在createMyClass()
默认方法中。这会导致代码重复,这是不可取的。
Therefore, this causes problem with refactoring, and to remove such code duplication, private default methods are allowed.
因此,这会导致重构出现问题,并且为了删除此类代码重复,允许使用私有默认方法。
Thanks for answering!
谢谢回答!
回答by mk.
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.
如果两个默认方法需要共享代码,私有接口方法将允许它们这样做,但不会通过接口公开该私有方法及其所有“实现细节”。
回答by maxim_ge
Why not simply (simply = using Java8):
为什么不简单(简单地=使用Java8):
PS: because of private helper is not possible in Java
PS:因为私人助手在Java中是不可能的
public interface MyInterface {
private static class Helper{
static 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();
Helper.initializeMyClass(myClass, params);
return myClass;
}
default MyClass createMyClass() {
MyClass myClass = new MyClass();
Helper.initializeMyClass(myClass, null);
return myClass;
}
}
回答by Dhiral Kaniya
Java 9 allows declaring a private method inside the interface. Here is the example of it.
Java 9 允许在接口内声明私有方法。这是它的例子。
interface myinterface {
default void m1(String msg){
msg+=" from m1";
printMessage(msg);
}
default void m2(String msg){
msg+=" from m2";
printMessage(msg);
}
private void printMessage(String msg){
System.out.println(msg);
}
}
public class privatemethods implements myinterface {
public void printInterface(){
m1("Hello world");
m2("new world");
}
public static void main(String[] args){
privatemethods s = new privatemethods();
s.printInterface();
}
}
For that, you need to update jdk up to 1.9 version.
为此,您需要将 jdk 更新到 1.9 版本。