[Java basics] the use of private methods of the interface

·


Preface

Problem description:
We need to extract a common method to solve the problem of duplicate code between the two default methods.
But this shared method should not be used by the implementation class. It should be privatized. The
solution:
Starting from java9, the interface allows to define private methods.
1. Ordinary private method, to solve the problem of code duplication between multiple methods
Format:
private return value type method name (parameter list) { method body } 2. Static private method, to solve the problem of code duplication between multiple static methods private static return Value type method name (parameter list) () { method body }





The use of private methods of the interface is to solve the problem of repetition in multiple static methods.


1. Application scenario of private method of interface?

code segment:

public interface MyInterfacePrivateA {
    
    
    public default void methodDeault1(){
    
    
        System.out.println("默认方法1");
        System.out.println("AAA");
        System.out.println("BBB");
        System.out.println("BBB");
    }
    public default void methodDeault2(){
    
    
        System.out.println("默认方法2");
        System.out.println("AAA");
        System.out.println("BBB");
        System.out.println("BBB");
    }
}

The usage scenario of the private method of the interface: methodDeault1() and methodDeault2() have a common output statement in the code (a common method is also available).

2. Correct writing

1. Rewrite the interface

The code is as follows (example):

public interface MyInterfacePrivateB  {
    
    
    public static void methodStatic1(){
    
    
        System.out.println("静态方法1");
        methodStaticCommon();
    }

    public static void methodStatic2(){
    
    
        System.out.println("静态方法2");
        methodStaticCommon();

    }
    //如果jdk不是java 9 此处会报错,报错内容“make not methodStaticCommon private”
    private static void methodStaticCommon(){
    
    
        System.out.println("AAA");
        System.out.println("BBB");
        System.out.println("BBB");
    }
}

2. Reference to the interface

The code is as follows (example):

Note: MyInterfacePrivateB.methodStaticCommon cannot be called, because this method is a private method and can only be called in this class or interface

public class Demo04Interface {
    
    
    public static void main(String[] args) {
    
    
        MyInterfacePrivateB.methodStatic1();;
        MyInterfacePrivateB.methodStatic2();
    }
}

# Interface content summary

In the Java 9+ version, the content of the interface can include:

1. Member variables are actually constants, format:

[public] [static] [final] Data type constant name = data value;

Constants must be assigned and cannot be changed once assigned.

Constant names are completely uppercase, separated by underscores.

2. The most important thing in the interface is the abstract method, the format:

[public] [abstract] Return value type method name (parameter list);

Note: The implementation class must override all abstract methods of the override interface, unless the implementation class is an abstract class.

3. Starting from Java 8, default methods are allowed to be defined in the interface, the format is:

[public] default return value type method name (parameter list) {method body}

Note: The default method can also be overridden

4. Starting from Java 8, static methods are allowed to be defined in the interface, the format is:

[public] static return value type method name (parameter list) {method body}

Note: It should be called by the interface name, and the static method of the interface cannot be called by the implementation class object

5. Starting from Java 9, it is very scarce to allow definition of private in the interface, the format:

Ordinary private method: private return value type method name (parameter list) {method body}

Static private method: private static return value type method name (parameter list) {method body}
Note: The private method can only be called by the interface itself, and cannot be used by the implementation class or others.

Guess you like

Origin blog.csdn.net/weixin_42556863/article/details/108490610