JDK8 new features 3-- function interface

Method 1. JDK8 two new interfaces

Before jdk8 Interface

interface 接口名 {
    静态常量;
    抽象方法
}

JDK8 to enhance the interface, the interface also can have default methods and static methods

JDK8 Interface:

interface {
    静态常量;
    抽象方法;
    默认方法;
    静态方法;
}

1.1 interface default method

1.1.1 Definition Format

interface 接口名{
    修饰符 default 返回值类型 方法名(){
        body
    }
}

1.1.2 Use

One way: class that implements the default method call interface directly

Second way: you may override the default implementation class interface method

For example: define three interfaces A, B, C, B, and C are implemented A

package com.hm.lambda;

public interface AA {
    public default void test01(){
        System.out.println("接口AA的默认方法");
    }
}



package com.hm.lambda;

// 实现类可以直接使用AA的默认方法
public class BB implements AA{
}


package com.hm.lambda;

// 实现类可以重写AA的默认方法
public class CC implements AA {
    @Override
    public void test01() {
        System.out.println("我是CC重写的方法");
    }
}

Then call the CC and BB respectively method

 public static void main(String[] args) {
        BB bb = new BB();
        bb.test01();
        CC cc = new CC();
        cc.test01();
    }

Run results shown in Figure:

1.2 Interface static methods

To facilitate the expansion, JDK8 interfaces added a static method

1.2.1 Definition Format

interface 接口名 {
    修饰符 static 返回值类型 方法名(){
        代码;
    }
}

1.2.2 Use

Directly use the interface name. Static method call to

E.g:

package com.hm.lambda;

public interface AA {
    public default void test01(){
        System.out.println("接口AA的默认方法");
    }

    public static void test02(){
        System.out.println("静态方法test02");
    }
}

transfer:

 public static void main(String[] args) {
        AA.test02();
    }

Note: The interface static method can not be inherited, can not be rewritten

1.2.3 Interface differences default method and a static method

1) The default method instance calls, static method calls through the interface name

2) The default method may be inherited, a default implementation class can use the interface direct method, the method may override the default

3) static method can not be inherited, the interface implementation class can not override static methods, you can only use the interface name calling

Summary: When will define the default method, when the definition of static methods? When this method needs to be inherited or overwritten when you use default method, otherwise use a static method.

2. Recognize function interface

        Functional Interface (FunctionalImplement) containing only an interface to an abstract method declarations, but may have a plurality of non-abstract methods of the interface. Similarly labeled Marker Interface in Java interface type, such as, etc. are not java.io.Serializable interface method declaration or attribute declaration, mainly for direct through instanceof can detect whether a particular instance of an interface instance.

        Interface is an interface function, the interface can have an abstract method, this type of interface is also called SAM interface.

@FunctionalInterface
public interface LamFilter {
    Boolean filter(Apple apple);
}

         Java 8 is a function interface introduces a new comment @FunctionalInterface, mainly used for error checking compiler level, with the comment, when you write the interface does not meet the functional interface definition, the compiler will complain. Plus without @FunctionalInterface no effect on the function of the interface is not the interface, the notes just to remind the compiler to check whether the interface contains only an abstract method. As illustrated, annotate @FunctionalInterface declared on the interface is an interface definition function interface.

2.1 Functional characteristics of the interface

The method allows to define the default 2.1.1

The default method is not abstract, there is a default implementation, called directly by implementing the class, the class does not need to implement this method to achieve improved scalability!

@FunctionalInterface
interface HelloWorld {
    void print(String str);
    //默认方法
    default void print2(String message) {
       System.out.println(message);
    }
}

Note: The default (default) method can only be called via the interface implementation class, it does not require implementation, that interface implementation class can inherit or override the default method.  

2.1.2 allows the definition of static methods

Static methods can not be abstract method, a method has been implemented, direct interface calls. With the ordinary static method is the same, it does not require implementation class to call

@FunctionalInterface
interface HelloWorld {
    void print(String str);
    //静态方法
    static void print3(String message) {
        System.out.println(message);
    }
}

Note: Static (static) method can only be invoked by the interface name, it can not be achieved by the class name of the class or object that implements the class is called. Just like normal static class methods, class where the method by direct call 

2.1.3. Java.lang.Object in the public method allows to define

        These methods for the interface functions, it is not treated as abstract method (although they are an abstract method); as a function implemented in any interface, the default inherit the Object class, containing from those in java.lang.Object implement the abstract methods;

2.1.4 allows multiple sub-interfaces inherit parent interface

        A plurality of interface inheritance allows sub-parent interface, but an abstract interface methods can only exist for each parent, and must be the same abstract methods

Note: the function interface, and only an abstract method, Object except public methods.

3. Custom Function Interface

3.1 number of custom function parameters Interface        

We found that, BiFunction JDK provides a maximum of only two parameters, if we have three parameters of how to do it? This time we need to use custom interface functions to achieve it!

E.g:

Orage.java

package com.bjc.demo1;

public class Orage {
    private  String color;
    private Double price;
    private Double weight;
    
    public Orage(String color, Double price, Double weight) {
        this.color = color;
        this.price = price;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }
}

 Custom function interface ThreeFunction:

package com.bjc.lambda.demo1;

@FunctionalInterface
public interface ThreeFunction<T,U,V,R> {
    R apply(T t,U u,V v);
}

Interface function using:

ThreeFunction<String,Double,Double,Orage> tFunc = Orage :: new;
Orage orage = tFunc.apply("red", 3.99, 0.78);
System.out.println(orage);

3.2 custom parameter-free function interface

Interface definition:

interface InterfaceExample{
	Example create(String name);
}

use:

public static void main(String[] args) {
	InterfaceExample com =  Example::new;
	Example bean = com.create("hello world");
	System.out.println(bean.name);
}

Note: Example is a class pojo

Published 205 original articles · won praise 9 · views 7920

Guess you like

Origin blog.csdn.net/weixin_43318134/article/details/104426168