好文档 - 专业文书写作范文服务资料分享网站

Java 8函数式接口functional interface的秘密

天下 分享 时间: 加入收藏 我要投稿 点赞

重庆达渝仁科技官网:www.cqdyr.com 4 5 6 7 8 9 10 11 12 13 14 15 16 17

default void say(String name) { System.out.println(\ } }

class FunctionalInterfaceWithDefaultMethod { public static void main(String[] args) { InterfaceWithDefaultMethod i = (o) -> {}; i.apply(null); i.say(\ } }

InterfaceWithDefaultMethod仍然是一个函数式接口。 泛型及继承关系 接口可以继承接口。 如果父接口是一个函数接口, 那么子接口也可能是一个函数式接口。 判断标准依据下面的条件: 对于接口I, 假定M是接口成员里的所有抽象方法的继承(包括继承于父接口的方法), 除去具有和Object的public的实例方法签名的方法, 那么我们可以依据下面的条件判断一个接口是否是函数式接口, 这样可以更精确的定义函数式接口。 如果存在一个一个方法m, 满足: ? ?

m的签名(subsignature)是M中每一个方法签名的子签名(signature) m的返回值类型是M中的每一个方法的返回值类型的替代类型(return-type-substitutable) 那么I就是一个函数式接口。

重庆达渝仁科技官网:www.cqdyr.com

看几个例子。

1)

1 interface X { int m(Iterable arg); } 2 interface Y { int m(Iterable arg); } 3 interface Z extends X, Y {}

接口Z继承了X,Y接口的m方法,由于这两个方法的签名相同,返回值也一样,所以Z有唯一的一个抽象方法int m(Iterable arg);,可以作为函数式接口。 2) 1 interface X { Iterable m(Iterable arg); } 2 interface Y { Iterable m(Iterable arg); } 3 interface Z extends X, Y {}

方法签名Y.m 既满足签名是X.m,并且返回值也满足return-type-substitutable。所以Z是函数式接口,函数类型为Iterable m(Iterable arg)。 3) 1 interface X { int m(Iterable arg); } 2 interface Y { int m(Iterable arg); } 3 interface Z extends X, Y {}

编译出错, 没有一个方法的签名是所有方法的子签名:

4)

重庆达渝仁科技官网:www.cqdyr.com 1 interface X { int m(Iterable arg, Class c); } 2 interface Y { int m(Iterable arg, Class c); } 3 interface Z extends X, Y {}

Compiler error: No method has a subsignature of all abstract methods 编译出错, 没有一个方法的签名是所有方法的子签名

5)

1 interface X { long m(); } 2 interface Y { int m(); } 3 interface Z extends X, Y {}

Compiler error: no method is return type substitutable 编译出错, 返回值类型不同。

6)

1 interface Foo { void m(T arg); } 2 interface Bar { void m(T arg); }

3 interface FooBar extends Foo, Bar {}

Compiler error: different signatures, same erasure 编译出错

7)

1 interface Foo { void m(String arg); } 2 interface Bar { void m(T arg); }

3 interface FooBar extends Foo, Bar {}

不是一个函数式接口, 两个方法的类型参数不一样。

重庆达渝仁科技官网:www.cqdyr.com 8) 1 interface X { void m() throws IOException; } 2 interface Y { void m() throws EOFException; }

3 interface Z { void m() throws ClassNotFoundException; } 4 interface XY extends X, Y {} 5 interface XYZ extends X, Y, Z {}

X.m,Y.m,Z.m方法签名相同,返回值类型都是void,只是异常列表不同。 EOFException是IOException的子类。 在这种情况下XY和XYZ都是函数式接口,但是函数类型不同。 // XY has function type ()->void throws EOFException // XYZ has function type ()->void (throws nothing) 9) 1 interface A {

2 List foo(List arg) throws IOException, SQLTransientException; 3 }

4 interface B {

5 List foo(List arg) throws EOFException, SQLException, TimeoutException; 6 }

7 interface C {

8 List foo(List arg) throws Exception; 9 }

10 interface D extends A, B {} 11 interface E extends A, B, C {}

// D has function type (List)->List throws EOFException, SQLTransientException

// E has function type (List)->List throws EOFException, SQLTransientException

重庆达渝仁科技官网:www.cqdyr.com 10) 1 interface G1 {

2 Object m() throws E; 3 }

4 interface G2 {

5 String m() throws Exception; 6 }

7 interface G extends G1, G2 {}

// G has function type ()->String throws F

函数式接口的交集 1)

1 2 3 4 5 6 7 8 9 10 11

public class Z { public static void main(String[] args) { Object o = (I & J) () -> {}; } }

interface I { void foo(); }

interface J { void foo(); }

I和J方法的交集依然符合函数式接口的定义。 上述代码可以用JDK中的javac编译通过但是Eclipse报错,这是Eclipse的一个bug。

2)

1 2 3

public class Z { public static void main(String[] args) { Object o = (I & J) () -> {};

Java 8函数式接口functional interface的秘密

重庆达渝仁科技官网:www.cqdyr.com4567891011121314151617defaultvoidsay(Stringname){System.out.println(\}}classFunctionalInterfaceWithDefault
推荐度:
点击下载文档文档为doc格式
9ih8i4tuxm35m4y30uzt
领取福利

微信扫码领取福利

微信扫码分享