Package com.wzy.t1; @FunctionalInterface//This annotation is used to declare that this interface is a functional interface Public Interfacepeople {/** * 1. A functional interface can have only one abstract method, not a single method, * because the Equals () method is implemented in Java.lang.Object, it also conforms to the functional specification * 2. If there is only one abstract method within this interface, So even if you do not write @functionalinterface, it is also considered a functional interface * 3. There can be many default methods * 4. Using the default keyword, you can define an instance method within an interface. * Note that this method is not an abstract method, but rather a concrete instance method with specific logic. * */ Public voideat (); //Public void Eat2 ();//there will be a second error . Publicboolean equals (Object obj);
Interface Default Methoddefault Public voidsleep () {System. out. println ("lie down and sleep."); } default Public voidrun () {System. out. println ("walking. "); }}
Package com.wzy.t1; Public Interface Student { publicvoid eat (); default void run () { System. out. println (" small run ");} }
Package com.wzy.t1; Public classMain implements people,student{ Public Static voidMain (string[] args) {NewMain (). run (); NewMain (). Eat (); NewMain (). Sleep (); } @Override Public voidrun () {//Multiple inheritance: When the same default method is present on two interfaces, you must redefine the interface yourself, or specify which default method to use for which interfacePeople.super.sleep (); People.super.run (); Student.super.run (); } @Override Public voideat () {System. out. println ("Big Stutter"); }}
java1.8-Functional Interface