Java interface and related knowledge collection


Read instructions

Code block  [   ]  The content in can be omitted

One, the introduction of the interface

  • interface(Interface), is an abstract type in the JAVA programming language, a collection of abstract methods, and interfaces are usually declared as interfaces. A class inherits the abstract methods of the interface by inheriting the interface.
  • Interface is aReference type, The most important content is among themAbstract method

Second, the definition and use of the interface

1. Definition

public interface 接口名称{
    
    
	//接口内容
	//抽象方法(接口的核心内容)
}

2. Use

Keywords to implement the interface:implements

public [abstract] class 类名称 implements 接口名称{
    
    
    //类的内容
    //重写抽象方法
}

【note】

1. Interface 不能直接实例化, you must use one 实现类来实现接口
2. InterfaceThe implementation class must be rewrittenInterfaceAll abstract methods
3. If 实现类没有重写所有接口中的抽象方法, then this 实现类一定是抽象类
4. InterfaceNo constructor and static code block

Third, the content allowed to be defined in the interface

java version What can be defined
java 7 constant
  Abstract method
java 8 Default method
  Static method
java 9 Private method

1. Constant

(1) Define the format

[public static final] 数据类型 常量名称 = 数据值;

(2) Use

接口名称.常量名;

【note】

  • Constants in the interface始终被public static final三个关键字修饰And cannot be changed, Can be omitted when writing code
  • constant定义之后必须赋值
  • Naming:全部字母大写 ,多个单词用 _ 分隔

2.抽象方法(重点)

(1) Define the format

[public abstract] 返回值类型 方法名称(参数列表);

【note】

  • Abstract method in interface始终被public abstract关键字修饰And cannot be changed, Can be omitted when writing code
  • Abstract method has no method body

(2) Examples of the use of abstract classes

Define interfaces and define abstract methods in various forms (Same effect

public interface InterfaceAbstract {
    
    

    //这是一个抽象方法
    public abstract void methodOne();
    //这是一个抽象方法
    abstract void methodTwo();
    //这是一个抽象方法
    public void methodThree();
    //这是一个抽象方法
    void methodFour();

}

Create an implementation class to implement an interface The
implementation class must rewrite all abstract methods in the interface

public class InterfaceAbstractImpl implements InterfaceAbstract{
    
    

    @Override
    public void methodOne() {
    
    
        System.out.println("第一个方法");
    }
    @Override
    public void methodTwo() {
    
    
        System.out.println("第二个方法");
    }
    @Override
    public void methodThree() {
    
    
        System.out.println("第三个方法");
    }
    @Override
    public void methodFour() {
    
    
        System.out.println("第四个方法");
    }
    
}

Create an implementation class object in the test class

public class Test01Interface {
    
    
    public static void main(String[] args) {
    
    
        InterfaceAbstractImpl interfaceAbstract = new InterfaceAbstractImpl();
        interfaceAbstract.methodOne();
        interfaceAbstract.methodTwo();
        interfaceAbstract.methodThree();
        interfaceAbstract.methodFour();
    }
}

operation result
Insert picture description here

3. The default method

The default method can be used for interface upgrades and splicing lambda expressions

(1). Defining the format

[public] default 返回值类型 方法名称(){
    
    
    方法体;
}

【note】

  • The default method in the interface始终被public关键字修饰And cannot be changed, Can be omitted when writing code
  • Default methods can be implemented class objects 直接调用, can be implemented classes重写

4. Static method

When the content of the method belongs to the class, static methods should be used

(1) Define the format

[public] static 返回值类型 方法名称(参数列表){
    
    
    方法体;
}

【note】

  • Abstract method in interface始终被public关键字修饰And cannot be changed, Can be omitted when writing code
  • Static methods can only be called by interface name(Avoid repeating static methods in multiple interfaces)

5. Private method

Methods that cannot be used by the implementation class should be privatized

(1) Define the format

Ordinary private method to
solve the duplicate code problem of multiple default methods

private 返回值类型 方法名称(参数列表){
    
    
    方法体;
}

Static private method to
solve the problem of duplicate code in private method

private static 返回值类型 方法名称(参数列表){
    
    
    方法体;
}

Four, multiple implementation and multiple inheritance of interfaces

1. Implement multiple interfaces

(1) Format

public class 实现类 implements 接口1, 接口2, ... {
    
    
    //重写所有抽象方法
}

(2) Code example

Define two interfaces

public interface InterfaceOne {
    
    
    public abstract void methodOne();
    void method();
}
public interface InterfaceTwo {
    
    
    public abstract void methodTwo();
    void method();
}

Create an implementation class, inherit two interfaces

public class InterfaceImpl /* extends Object */implements InterfaceOne,InterfaceTwo{
    
    
    @Override
    public void methodOne() {
    
    
        System.out.println("InterfaceOne和InterfaceTwo接口中重复的method方法只需要在实现类中重写一次");
    }
    @Override
    public void method() {
    
    
        System.out.println("重写了InterfaceOne接口中的methodOne方法");
    }
    @Override
    public void methodTwo() {
    
    
        System.out.println("重写了InterfaceTwo接口中的methodTwo方法");
    }
}

【note】

1. a class 直接父类是唯一的, but 可以同时实现多个接口
2. If 多个接口出现重复抽象方法, implementation class 只需要覆盖重写一个to
3. IfDid not implement all the abstract methods, thenThe implementation class must be an abstract class ==
4. If there are multiple interfaces 存在重复的默认方法, then 实现类必须对重复的默认方法进行重写
5. A class if the methods in the direct parent class and the interfaceDefault method repeat,meetingPrefer the abstract method of the direct parent class(The priority of the parent class method is greater than the interface)

2. Multiple inheritance of interfaces

(1) Format

public interface 接口 extends 接口1, 接口2, ...{
    
    
	//接口内容
}

(2) Multiple inheritance examples

Define two interfaces

public interface InterfaceOne {
    
    
    void methodOne();
}
public interface InterfaceTwo {
    
    
    void methodTwo();
}

Define one interface to inherit two interfaces

public interface InterfaceTest extends InterfaceOne,InterfaceTwo{
    
    
    void method();
}

【note】

1. If the abstract methods in multiple interfaces are repeated, it doesn’t matter
2. If there are multiple parent interfacesDefault method repeat,thenThe subinterface must carry the default keyword to rewrite the repeated default method

Five, the interface type as the parameter and return value of the method

//定义接口
interface Interface {
    
    
    //接口中的test抽象方法
    void test();
}

//创建测试类
public class Test {
    
    

    public static void main(String[] args) {
    
    
        //使用匿名内部类创建接口对象,作为参数
        Interface anInterface = new Interface() {
    
    
            @Override
            public void test() {
    
    
                System.out.println("这是接口类型作为方法的参数");
            }
        };
        //向method方法传递参数,并调用接口中的test方法
        method(anInterface).test();
    }

	//声明一个以Interface为参数和返回值类型的方法
    public static Interface method(Interface para){
    
    
        //使用参数调用test()方法
        para.test();
        //使用匿名对象的匿名内部类作为方法的返回值
        return new Interface() {
    
    
            @Override
            public void test() {
    
    
                System.out.println("这是接口类型作为方法的返回值");
            }
        };
    }

}

operation result
Insert picture description here

Six, interface and class

The following quote from rookie Tutorial - Java Interface

1. Similarities between interfaces and classes

An interface can have multiple methods.
The interface file is saved in a file ending in .java, and the file name uses the interface name.
The bytecode file of the interface is saved in a file ending in .class.
The bytecode file corresponding to the interface must be in the directory structure that matches the package name.

2. The difference between interface and class

Interfaces cannot be used to instantiate objects.
There is no constructor for the interface.
All methods in the interface must be abstract methods.
The interface cannot contain member variables.
The interface is not inherited by the class, but to be implemented by the class.
The interface supports multiple inheritance.

3. Interface and abstract class

The method in the abstract class can have a method body, that is, it can realize the specific function of the method, but the method in the interface cannot.
The member variables in the abstract class can be of various types, while the member variables in the interface can only be of the public static final type.
The interface cannot contain static code blocks and static methods (java 8+ can), while abstract classes can have static code blocks and static methods.
A class can only inherit one abstract class, but a class can implement multiple interfaces.

Guess you like

Origin blog.csdn.net/weixin_44580492/article/details/106630335