[英]How to return a lambda function and use by passing as an argument to a method in Java 8? Why I can't use :: operator?
[英]Why can't I use a method in a lambda?
碼:
source.subscribe(
string -> {
if( observed ) destination.onNext( reverse( string ) );
}
);
private String reverse( String forward ) {
return new StringBuilder( forward ).reverse().toString();
}
source
被聲明為ObservableSource<String>
。
destination
是Observer<String>
。
Android Studio中強調string
在調用reverse
,並告訴我“ reverse(java.lang.String)
在StringReverser
(我的封閉類)不能被應用到(<lambda parameter>)
”。
我不明白 source
傳遞字符串, destination
傳遞字符串。 而reverse
方法采用String參數。 有什么問題?
正如@Andreas在評論中指出的那樣,我將RxJava 2 Observer
視為舊式RxJava 1 Subscriber
。 不起作用! 一旦我實現了所有4個Observer
方法( onSubscribe(), onNext(), onError() and onComplete()
),錯誤就會消失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.