1.jdk8 new feature default keyword learning

  • Before jdk1.8, there can only be abstract methods for accessing, and no methods can be implemented.
  • jdk1.8 breaks this rule and introduces a new keyword default, using the default modification method, which can be achieved by defining specific methods in the interface
  • Default method: The interface defines a default method. After the implementation class of this interface implements this interface, it can be directly invoked regardless of the method of the default modification, that is, the default interface method achieve
public interface Person{
    void run();
    default void eat(){
        System.out.println("吃饭");
    }
}
  • Static method: access name. Static method to access the static method in the interface
public interface Person {
    void run();

    default void eat() {
        System.out.println("吃饭");
    }

    static void test() {
        System.out.println("这是静态⽅法");
    }
}

 

Guess you like

Origin blog.csdn.net/qq_37790902/article/details/103309024