How to use Java interface? Detailed explanation of new features of JDK8 interface

The interface can be understood as a specification. The members in the interface are all modified by public, whether they are written or not, because the purpose of the specification is to be public. Prior to Java JDK8, interfaces could only contain abstract methods and constants, without other components. However, it should be noted that interfaces cannot be instantiated.

Interface usage:

Interfaces are used to be implemented by classes, and the class that implements the interface is called the implementing class. Implementation classes can be understood as so-called subclasses.

修饰符 class 实现类 implements 接口1, 接口2, 接口3 , ... {
}
实现的关键字:implements

As can be seen from the above, an interface can be implemented by a single class or by multiple classes, that is, an interface can inherit one interface or multiple interfaces at the same time. However, if a class implements an interface, all abstract methods of the entire interface must be rewritten, otherwise the class needs to be defined as an abstract class.

New method of JDK8 start interface

default method

Similar to the ordinary instance method written before, it must be modified with default, and it will be modified with public by default. It needs to be called with an object of the implementation class of the interface.

default void run(){
   System.out.println("--开始跑--");
}

static method

By default, it will be modified as public and must be modified as static. The static method of the interface must be called with its own interface name, and the static method implementation can refer to the following methods.

static void inAddr(){
   System.out.println("我们都在黑马培训中心快乐的学习Java!");
}

private method

A private method is a private instance method: it must be modified with private, which has only been available since JDK 1.9. It can only be accessed by other default methods or private methods in this class.

private void go(){
    System.out.println("--准备--");
}

After the JDK8 version started, Java only added new member methods of the interface, allowing the interface to directly define methods with method bodies, because the subcode may be changed after the interface is enriched.
For example, in the following project, Version 1.0 is successfully launched without any problem.

public interface Inter {
    …若干抽象方法
}
public class InterImplA implements Inter {}
public class InterImplB implements Inter {}

In the project Version2.0, it is necessary to expand the function and enrich the Inter interface, so 10 new abstract methods are added. At this time, all the implementation classes must implement these methods when the interface is changed.

public interface Inter {
    …若干抽象方法
}
public class InterImplA implements Inter {}
public class InterImplB implements Inter {}

It is necessary to enrich the function of the interface without changing the code of the subclass. It is necessary to allow methods with method bodies to be directly defined in interfaces.

Note: The 3 new methods added by JDK8 are rarely used in development. They are usually related to the Java source code. We need to understand, recognize the syntax, and understand the calling relationship.

Supongo que te gusta

Origin blog.csdn.net/Blue92120/article/details/132601271
Recomendado
Clasificación