java1.8-Functional Interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.