简体   繁体   English

私有接口方法,示例用例?

[英]Private interface methods, example use-case?

"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/213 and 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.但是假设我们想在这两种方法中使用initializeMyClass(MyClass, Params) However, if we do it like this (default method), then initializeMyClass(MyClass, Params) will be part of the public interface!但是,如果我们这样做(默认方法),那么initializeMyClass(MyClass, Params)将成为公共接口的一部分! To prevent that from happening, we can only keep the code of entire initializeMyClass(MyClass, Params) inside the createMyClass() default methods.为了防止这种情况发生,我们只能将整个initializeMyClass(MyClass, Params)的代码保存在createMyClass()默认方法中。 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.因此,这会导致重构出现问题,并且为了删除此类代码重复,允许使用私有默认方法。

Thanks for answering!谢谢回答!

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.如果两个默认方法需要共享代码,私有接口方法将允许它们这样做,但不会通过接口公开该私有方法及其所有“实现细节”。

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

Java 9 allows declaring a private method inside the interface. Java 9 允许在接口内声明私有方法。 Here is the example of it.这是它的例子。

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 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM