Java8 functional interface/lambda expression/interface default method/interface static method/interface conflict method override/lambda expression specifying generic type, etc.

One: functional interface

1. The concept of functional interface is that this interface must have and can only have one abstract method, which can be specified by @FunctionalInterface (similar to @Override), but an interface without this annotation but with only one abstract method is also a functional interface; (Interfaces also have package access like classes, but internal methods are public by default)

@FunctionalInterface
public interface IFoo{
    void print();
} // It is the simplest functional interface, but if there is an abstract method such as void print2(), this interface is not a functional interface and an error will be reported at compile time (because of @FunctionalInterface)

2. There can be static methods in the interface (a new feature of JDK8)

@FunctionalInterface
public interface IFoo{
    void print();
    static void sTest(){
      System.out.println("msg");
    }      
}

Note that the static methods in the interface can only be public by default, just like the fields in the interface can only be public static by default; when calling, you can directly IFoo.sTest();

3. There can also be default methods in the interface (new features of JDK8)

@FunctionalInterface
public interface IFoo{
    void print();
    default void sTest(){
      System.out.println("msg");
    }      
}

Unlike the static method, the default method is essentially an instance method, so it is still necessary to create an implementation class object of IFoo to call this method, such as (new IFoo(){...}).sTest(); It is possible but IFoo .sTest() is wrong

4. When an interface becomes a functional interface and is also used as a functional interface, it is similar to the delegate in C#, that is, the interface and the abstract methods in it are in one-to-one correspondence, so the declaration of generics should be in the interface. in the name not the abstract method

@FunctionalInterface
public interface IFoo<T extends Serializable>{
    T print(T arg);   // Writing as <T> T print(T); is a wrong way to think of IFoo as a delegate; note <T extends B> T print. . It is also possible in ordinary interfaces; 
    static  void sTest(){
      System.out.println("msg");
    }      
}

5. The relationship between functional interface objects and their lambda expressions

The lambda expression will not generate the .class file of the class inside, but new Predicate(){...} will be generated, so if you need to convert the lambda expression to an interface object, you need to cast it, such as: (( Predicate) o -> false).and(o -> false)

The above (o -> false) is a "function object", which is different from new Predicate(){@Override test...}. The former does not generate a Predicate implementation class, so a forced type conversion is required Convert the result produced by the lambda expression into an interface implementation class object;

6. Use functional interfaces as delegates

public interface Println<T>{
    void println(T msg);
}

Can be assigned by Println<String> println = System.out::println;;

7. When implementing multiple interfaces but the default method with the same name, the method must be rewritten/redefined (it doesn’t matter if it is an abstract method. When a subclass implements IC and implements print, it actually implements IA and IB does not exist. statement to be rewritten twice)

public interface IA{
    default void print(){}
}
public interface IB{
    default void print(){}
}
public interface IC extends IA, IB{
    @Override
    default  void print(){ IA.super .print();}   // TODO is used to explicitly specify the print with IA. Note that before JDK8, there is no default and only one class can be inherited, so only super.print() is needed. can 
}

8. Type deduction for lambda expressions

If it is a non-generic type, it is naturally not required, but if the parameter is a generic type but cannot be implicitly deduced, you can specify the type of the generic type through (String s) -> {..};

9. There can be default methods in the interface. At this time, the interface can directly generate objects with new ITest{}, but it can also override the default method.

10. You can rewrite the default method of the parent interface in the subinterface to make it an abstract method in the subinterface;

11. Difference between lambda expression and anonymous inner class

A lambda expression is a very special existence. It does not generate a .class file, and it is not an object of the implementation class of the interface. It should be understood that IFoo foo = ()->true; IFoo can match the lambda expression on the right, Instead of saying that this regular expression is an object of an anonymous implementation class of IFoo;

Note that there is an implicit conversion above i.e. IFoo foo = (IFoo) ()->true;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325007006&siteId=291194637