RxJava学习(一)---Hello World

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HUandroid/article/details/80344517

学习一种新的编程思想或者语音,先来个Hello World。写一个RxJava的Hello World:

public static void main(String[] args){
        Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("Hello World");
            }
        }).subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println(s);
            }
        });
    }

看着有点长是吧,换个简写的:

Observable.just("Hello World").subscribe(new Consumer<String>() {
            @Override
            public void accept(String s) throws Exception {
                System.out.println(s);
            }
        });

使用Java8的Lambda表达式更简单:

Observable.just("Hello World").subscribe(System.ot::println);

这里不建议使用Lambda表达式,过于简化细节,对代码阅读有影响。

猜你喜欢

转载自blog.csdn.net/HUandroid/article/details/80344517