6. RxJava 库的功能接口
类 Observable 是异步库 RxJava 的基础。它与我们刚刚介绍的类 Stream 具有许多相似之处。与后者一样,它提供了大量的方法,多达数十个。 同样地,这些方法也接受函数式接口的实例作为参数。我们将首先介绍其中最常用的两个:[Actioni] 和 [Funci]。
6.1. 示例-01:功能接口 [Action0]
![]() |
[Action0] 接口如下:
![]() | ![]() |
- 接口 [Action0] 仅包含一个方法 call,其签名与 [2] 相同:void call()。它继承自接口 [Action] 和 [1, 3]。 接口 [Action] 没有方法。此类接口被称为标记接口 [4]。它们主要用于编写:if(c instanceOf(Action)) {...}。 接口 [Action] 本身继承自标记接口 [Function] [4-6];
![]() |
我们将通过以下代码(示例01)演示 [Action0] 接口:
package dvp.rxjava.lamdbas;
import rx.functions.Action0;
public class Exemple01 {
public static void main(String[] args) {
// 操作0
Action0 a01 = new Action0() {
@Override
public void call() {
System.out.println("anonymous a01.call");
}
};
Action0 a02 = () -> System.out.println("lambda ao2.call");
// 展示量
a01.call();
a02.call();
}
}
- 第 8-13 行:使用匿名类实现 [Action0] 接口;
- 第 8-13 行:使用 lambda 表达式实现 [Action0] 接口;
结果如下:
6.2. 示例-02、03:功能接口 [Actioni]
![]() |
接口 [Action1] 如下所示:
![]() |
我们将通过以下代码(示例02)进行说明:
package dvp.rxjava.lamdbas;
import rx.functions.Action1;
public class Exemple02 {
public static void main(String[] args) {
// 操作1
Action1<Integer> a10 = new Action1<Integer>() {
@Override
public void call(Integer integer) {
System.out.printf("anonymous a10.call (%s)%n", integer);
}
};
Action1<Double> a11 = (d -> System.out.printf("lambda a11.call (%s)%n", d));
// 执行次数
a10.call(20);
a11.call(17.4);
}
}
其结果如下:
一般而言,[Actioni] (0<=i<=9) 是一个功能接口,其唯一的方法签名如下:void call(T1 t1, T2 t2, ..., Ti ti)。 以下是 [Action2] 的示例(示例03):
package dvp.rxjava.lamdbas;
import rx.functions.Action2;
public class Exemple03 {
public static void main(String[] args) {
// 操作2
Action2<Integer, Double> a20 = new Action2<Integer, Double>() {
@Override
public void call(Integer integer, Double aDouble) {
System.out.printf("anonymous a20.call(%s,%s)%n", integer, aDouble);
}
};
Action2<Integer, Double> a21 = (i, d) -> System.out.printf("lambda a21.call(%s,%s)%n", i, d);
// 执行
a20.call(10,11.3);
a21.call(5,5.6);
}
}
所得结果如下:
6.3. 示例-04、05:功能接口 [Funci]
![]() |
接口 [Func0] 如下所示:
![]() |
此次,该接口的 [call] 方法返回 R 类型的结果。
我们将通过以下代码演示该接口:
package dvp.rxjava.lamdbas;
import rx.functions.Func0;
public class Exemple04 {
public static void main(String[] args) {
// Function0
Func0<String> f00 = new Func0<String>() {
@Override
public String call() {
return "anonymous f00.call";
}
};
Func0<String> f01 = () -> "lambda f01.call";
// 执行
System.out.println(f00.call());
System.out.println(f01.call());
}
}
其返回结果如下:
一般而言,[Funci] (0<=i<=9) 是一个功能接口,其唯一的方法签名如下:R call(T1 t1, T2 t2, ..., Ti ti)。 以下是 [Func2] 的示例(示例05):
package dvp.rxjava.lamdbas;
import rx.functions.Func2;
public class Exemple05 {
public static void main(String[] args) {
// Func2
Func2<Integer, Double, String> f20 = new Func2<Integer, Double, String>() {
@Override
public String call(Integer integer, Double aDouble) {
return String.format("anonymous f20.call(%s,%s)", integer, aDouble);
}
};
Func2<Integer, Double, String> f21 = (i,d) -> String.format("anonymous f21.call(%s,%s)", i, d);
// 显示
System.out.println(f20.call(10,10.2));
System.out.println(f21.call(100, 100.3));
}
}
其结果如下:







