Private interface methods, example use-case?

后端 未结 3 1019
借酒劲吻你
借酒劲吻你 2020-12-30 19:36

\"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 with

3条回答
  •  有刺的猬
    2020-12-30 20:23

    Why not simply (simply = using Java8):

    PS: because of private helper is not possible in 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;
     }
    }
    

提交回复
热议问题