[Java Interface

Interface Description

Interface specification is a common multiple classes.
The interface is a data type, is one of the most important elements of reference: the abstract methods .

How to define the format of an interface:

public interface 接口名称 {
    // 接口内容
}

NOTE: After you replace the keyword interface, the compiler generates bytecode file is still: .java -> .class.

If Java 7, then the contents of the interface that can be included are:

  1. constant
  2. Abstract method

If Java 8, may additionally contain:

  1. The default method
  2. Static method

If Java 9, can additionally contain:

  1. Private methods

Step interface

  1. Interface can not be used directly, there must be a "implementation class" to "implement" the interface.
    Format:
    public class implements the interface implementation class name name {
    // ...
    }
  2. Class that implements the interface must overwrite (implemented) all abstract methods interface.
    Implementation: remove the abstract keyword, plus the method body braces.
  3. Create an object implementation class, use.

Note :
If the class does not implement all of the abstract methods interface overwritten, then the implementation class and that they must be abstract class.

Abstract interface methods

/*
在任何版本的Java中,接口都能定义抽象方法。
格式:
public abstract 返回值类型 方法名称(参数列表);

注意事项:
1. 接口当中的抽象方法,修饰符必须是两个固定的关键字:public abstract
2. 这两个关键字修饰符,可以选择性地省略。(今天刚学,所以不推荐。)
3. 方法的三要素,可以随意定义。
 */
public interface MyInterfaceAbstract {

    // 这是一个抽象方法
    public abstract void methodAbs1();

    // 这也是抽象方法
    abstract void methodAbs2();

    // 这也是抽象方法
    public void methodAbs3();

    // 这也是抽象方法
    void methodAbs4();

}

Interface default method

  1. The default interface method, it can interface class object, called directly.
  2. The default method of interface, the interface can also be implemented classes overwrite.

Starting Java 8, where the interface allows you to define the default method.
format:

public default 返回值类型 方法名称(参数列表) {
    方法体
}
public interface MyInterfaceDefault {

    // 抽象方法
    public abstract void methodAbs();

    // 新添加了一个抽象方法
//    public abstract void methodAbs2();

    // 新添加的方法,改成默认方法
    public default void methodDefault() {
        System.out.println("这是新添加的默认方法");
    }

}

NOTE: The default interface method which can solve the problem of the interface upgrades.

Static method interface

Note: The object can not be achieved through the interface class to the static method among the interface calls.
RIGHT: The interface name by directly calling the static method of them.
Format:
Interface name of a static method name (parameter);.

/*
从Java 8开始,接口当中允许定义静态方法。
格式:
public static 返回值类型 方法名称(参数列表) {
    方法体
}
提示:就是将abstract或者default换成static即可,带上方法体。
 */
public interface MyInterfaceStatic {

    public static void methodStatic() {
        System.out.println("这是接口的静态方法!");
    }

}

Private interface methods:

Ordinary private methods and private static methods

/*
问题描述:
我们需要抽取一个共有方法,用来解决两个默认方法之间重复代码的问题。
但是这个共有方法不应该让实现类使用,应该是私有化的。

解决方案:
从Java 9开始,接口当中允许定义私有方法。
1. 普通私有方法,解决多个默认方法之间重复代码问题
格式:
private 返回值类型 方法名称(参数列表) {
    方法体
}

2. 静态私有方法,解决多个静态方法之间重复代码问题
格式:
private static 返回值类型 方法名称(参数列表) {
    方法体
}
 */
public interface MyInterfacePrivateA {

    public default void methodDefault1() {
        System.out.println("默认方法1");
        methodCommon();
    }

    public default void methodDefault2() {
        System.out.println("默认方法2");
        methodCommon();
    }

    private void methodCommon() {
        System.out.println("AAA");
        System.out.println("BBB");
        System.out.println("CCC");
    }

}

Constant Interface

/*
接口当中也可以定义“成员变量”,但是必须使用public static final三个关键字进行修饰。
从效果上看,这其实就是接口的【常量】。
格式:
public static final 数据类型 常量名称 = 数据值;
备注:
一旦使用final关键字进行修饰,说明不可改变。

注意事项:
1. 接口当中的常量,可以省略public static final,注意:不写也照样是这样。
2. 接口当中的常量,必须进行赋值;不能不赋值。
3. 接口中常量的名称,使用完全大写的字母,用下划线进行分隔。(推荐命名规则)
 */
public interface MyInterfaceConst {

    // 这其实就是一个常量,一旦赋值,不可以修改
    public static final int NUM_OF_MY_CLASS = 12;

}

Note the use of the interface

  1. The interface is not a static block of code or method of construction.
  2. A direct parent class is unique, but a class can implement multiple interfaces simultaneously.
    Format:
    public class MyInterfaceImpl the implements MyInterfaceA, MyInterfaceB {
    // covers all abstract methods override
    }
  3. If multiple interfaces among the class implements, repeated abstract methods exist, then only overwritten once.
  4. If the implementation does not cover all abstract methods among all interfaces rewrite class, the implementation class must be an abstract class.
  5. If the class locks for multiple interfaces among duplicate default method, then the class must be implemented to overwrite the default method of conflict.
  6. If a class method among the direct parent, and the default method of generating an interface among a conflict among the parent class with priority.

Classes, inheritance and interface implementation issues

  1. Between classes is a single inheritance. Only one direct parent class.
  2. Between classes and interfaces are implemented multiple. A class can implement multiple interfaces.
  3. Between the interface and the interface is multiple inheritance.

Precautions:

  1. Abstract method, if an interface among a plurality of parent repeated, it does not matter.
  2. The default method interfaces among multiple parents if repeated, then the sub-interface must override the default method of rewriting, [and] with the default keyword.
Published 218 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/u011035397/article/details/104991987