I want to tell you: private methods can be defined in java interface

In traditional Java programming, a well-known point of knowledge is that private methods cannot be defined in the java Interface interface . Only allow us to define public access methods, abstract methods or static methods. But starting from Java 9, the Interface allows the definition of private methods and private static methods. Below we will introduce its grammar rules and why there is such a design.

In fact, before Java 8, there is another widely known knowledge point: all methods in the interface must be abstract methods. However, starting from Java 8, non-abstract methods can be included in the interface, such as the default method in the following text. This is not what we want to introduce to you in this article. If you are not familiar with it, please make up your own lesson.

One, Java 9 interface defines private methods

Starting from Java 9, we can add private private methods and private static methods to the Interface interface. These private methods will improve the reusability of the code inside the interface. For example, if two default methods are needed to share code, the private interface method will allow them to share code, but will not expose the private method to its implementation class calls (I will give you an example later).

There are four rules for using private methods in interfaces:

  • Private interface method can not be abstract abstract methods. Because abstract abstract methods are public methods used to implement interface implementation classes, they cannot be private.
  • Private methods in an interface can only be called from methods inside the interface.
  • Private static methods in the interface can be used in other static and non-static interface methods.
  • Private non-static methods in an interface cannot be used inside private static methods.
interface CustomInterface {

    public abstract void abstractMethod();  //抽象方法不能是私有的

    public default void defaultMethod() {
        privateMethod(); //可以调用接口中的私有方法
        privateStaticMethod(); //可以调用接口中的私有静态方法
        System.out.println("普通方法被调用");
    }

    public static void staticMethod() {
        privateStaticMethod(); //public静态方法可以调用private静态方法
        System.out.println("静态方法被调用");
    }

    private void privateMethod() {
        System.out.println("private私有方法被调用");
    }

    private static void privateStaticMethod() {
        System.out.println("private私有静态方法被调用");
    }
}

According to the above four rules, the above code definitions are correct

2. An example: Calculate the sum of odd and even numbers separately

The interface is defined as follows. In the following, the add method uses java8's Stream operation, using lambda expressions as filter conditions, and summing. The core is: addEvenNumbers even number sum function and addOddNumbers odd number sum function, both call the add interface private method.

public interface CustomCalculator 
{
    default int addEvenNumbers(int... nums) { //非抽象,java8 开始可以定义default方法
        return add(n -> n % 2 == 0, nums);   //过滤偶数并求和,调用private私有方法
    }

    default int addOddNumbers(int... nums) { //非抽象,java8 开始可以定义default方法
        return add(n -> n % 2 != 0, nums);  //过滤奇数并求和,调用private私有方法
    }

    //按照过滤条件过滤奇数或偶数并sum求和:java9开始可以定义private私有方法
    private int add(IntPredicate predicate, int... nums) { 
        return IntStream.of(nums)   //java8 Stream流
                .filter(predicate)   //java8 predicate及过滤器
                .sum();  //sum求和
    }
}

The interface implementation class MainCalculator implements the CustomCalculator interface

public class MainCalculator implements CustomCalculator {

    public static void main(String[] args) {
        CustomCalculator demo = new MainCalculator ();

        int sumOfEvens = demo.addEvenNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfEvens);   //过滤所有偶数并求和,结果是20

        int sumOfOdds = demo.addOddNumbers(1,2,3,4,5,6,7,8,9);
        System.out.println(sumOfOdds);   //过滤所有奇数并求和,结果是25
    } 
}

Welcome to follow my blog, there are many boutique collections

  • This article is reproduced indicate the source (en must not turn only the text): letters Gebo off .

If you think it is helpful to you, please like and share it for me! Your support is my inexhaustible creative motivation! . In addition, the author has output the following high-quality content recently, and I look forward to your attention.

Guess you like

Origin blog.csdn.net/hanxiaotongtong/article/details/109043531