Java Series Tutorial Java 8 (one)--functional interface

Source: Internet
Author: User

Introduction

At present, as the system has been completely switched to JDK8, it is necessary to systematically understand some of the new features of JAVA8, so that later in the daily work can use some advanced features to improve the efficiency of programming.

Because JAVA8 introduces a functional interface, the java.util.function contains several large classes of function-type interface declarations. The first part of this article focuses on function-related interfaces.

Functionalinterface annotations

java8 's new introduction, including functional design, interfaces have @FunctionalInterface the annotation. Just like the note description of this annotation, it is annotated at the interface level, and the annotated interface must have only one abstract method. Specifically, the annotations are on inteface, and there can be only one abstract method in the interface, with the default method. Because semantically speaking, a functional interface needs to express a single function through a logical approach. It's important to understand this single, not to limit you to a interface only an abstract method, the other methods of multiple methods need to inherit from the public method of object, or if you want to bypass, you implement default. The functional interface itself must be only an abstract method. Also, if the public method of the object class is not allowed. The official instructions are translated as follows:

If an interface i,i has a set of abstract method sets M, and none of these methods are public signature methods of the object class, then if there is a method m in m, then it satisfies:

  • The signature of M is a sub-signature of all method signatures in M.

  • M for each method in M is the return type replaceable. At this point, interface I is a function-type interface.

How to understand, see a few examples.

For example: You declare an interface:

@FunctionalInterfacepublic interface Func {}

This compiles incorrectly and the compiler will tell you *no target method*. And if you add a method:

@FunctionalInterfacepublic interface Func {void run ();}

This is OK, a functional interface is declared OK. And one more?

@FunctionalInterfacepublic interface Func {void run (); void foo ();}

Not OK, it is clear that there is only one abstract method. But if you change a function signature:

@FunctionalInterfacepublic interface Func {boolean equals (Object obj);}

The error is still the same because the method signature is the public method of the object class. And then change it:

@FunctionalInterfacepublic interface Func {boolean equals (Object obj); void run ();}

That's OK. An abstract method, a public method of object, is peaceful. There are other methods of object, how about the Clone method?

@FunctionalInterfacepublic interface Func {Object clone (); void run ();}

This is not the case, because it is clear that, if the public method of object, and clone is protected.

So the conclusion is:

A functional interface, with and only one abstract method, except for the public method of object.

Because Java itself supports multi-interface implementations, you define a class that can implements multiple interface. So this limitation also has no effect, if you want to contract a functional interface to unify, you can also do some default implementation to achieve the purpose of an interface multiple abstract methods, such as the following approach:
A common interface Nonfunc:

public interface Nonfunc {void foo (); void Voo ();}

Function-Type interface func:

@FunctionalInterfacepublic interface Func extends Nonfunc {void run (); default void foo () {//do something} default void Voo () {//Do something}}

Implementation of the Test class:

public class testj8functionalinterface implements func {     Public static void main (String[] args)  {         func func = new testj8functionalinterface ();         func.run ();         func.foo ();         func.voo ();    }     @Override      public void run ()  {         System.out.println ("Run");    }     @Override      Public void foo ()  {        system.out.println ("foo");     }     @Override     public void voo ()  {       &nbSp System.out.println ("Voo");     }}

Public class testj8functionalinterface2 {    public static void  main (String[] args)  {         Testj8functionalinterface2 testj8functionalinterface2 = new testj8functionalinterface2 ();         // lambda         Testj8functionalinterface2.test (10,  ()  -> system.out.println ("A customed Func.");         // method reference         testj8functionalinterface2.test (100, testj8functionalinterface2::customedfunc);     }    public void customedfunc ()  {         system.out.println ("a customed method reference.");     }    public voId test (Int x, func func)  {         SYSTEM.OUT.PRINTLN (x);         func.run ();     }}

The above example lists a lambda pattern and a method reference pattern, so that you can take advantage of the powerful ability of functional programming to use the method as a parameter.

To read the full text, please click: http://click.aliyun.com/m/9152/


This article is from the "12466150" blog, please be sure to keep this source http://12476150.blog.51cto.com/12466150/1890430

Java Series Tutorial Java 8 (one)--functional interface

Related Article

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.