我的代码中的 <T> 在那里做什么?

What is the <T> in my code doing there?

这是 的延续。我的问题已经解决了,但我想进一步询问代码,我最好再开一个问题,因为我还需要问一些其他的事情。所以这是我的观察

public static <T> Observable<T> makeObservable(final Callable<T> func) {
        return Observable.create(
                new Observable.OnSubscribe<T>() {
                    @Override
                    public void call(Subscriber<? super T> subscriber) {
                        try {
                            T observed = func.call();
                            if (observed != null) { // to make defaultIfEmpty work
                                subscriber.onNext(observed);
                            }
                            subscriber.onCompleted();
                        } catch (Exception ex) {
                            subscriber.onError(ex);
                        }
                    }
                }).map(new Func1<T, T>() {
            @Override
            public T call(T t) {
                return null;
            }
        });

不知道那个T在那儿干什么?我试着用谷歌搜索,但我无法理解它在上面代码中的作用。官方 java 文档说它是 'A generic type is a generic class or interface that is parameterized over types'。试图进一步搜索它,我发现了 this question that confuses me even more.

除了要真正了解那里的T,还要知道如何使用.map来转换T的内容。从上面的代码来看,这段

.map(new Func1<T, T>() {
    @Override
    public T call(T t) {
        return null;
    }

是我添加的,因为我想尝试将 T 的内容更改为其他内容,也许更改为另一个对象。我该如何实现?在此之前,这是我对RxJava中.map函数的理解。

根据 的官方文档,它说,

public final <R> Observable<R> map(Func1<? super T,? extends R> func)
Returns an Observable that applies a specified function to each item emitted by the source Observable and emits the results of these function applications.

func - a function to apply to each item emitted by the Observable

我从上面的 func 定义中解释正确吗,第一个参数(super T)应该是 'incoming' 数据类型,第二个参数(扩展 R)是 'outgoing' 数据类型,或转换项的数据类型。因为这是我从大多数代码示例中看到的。例如,来自

Observable.just("Hello, world!")
    .map(new Func1<String, Integer>() {
        @Override
        public Integer call(String s) {
            return s.hashCode();
        }
    })

从上面的代码我的理解是Func1接收字符串并将其传递给call(String s),以及returns Integer,定义在Func1<String, Integer>中。那么我上面的解释总是正确的吗?因为从我的第一个代码开始,我需要将 T 更改为其他内容,但是当我将 .map new Func1 的第二个参数更改为 T 以外的任何其他内容时,Android Studio 会给我 "Incompatible types" 错误.

T 是您的通用类型。这个link应该有助于解释它:https://docs.oracle.com/javase/tutorial/java/generics/types.html

基本上类型将在运行时被替换为真实类型,例如 String 或 Object。

如果您将 Func 的 return 类型从 T 类型更改为其他类型,那么您还必须修改 Observable 泛型类型。这在map的定义中可以看出:Observable类型R与Funcreturn类型R一致。

所以您现在正在处理泛型类型。您在这里拥有的是一个泛化方法,因此它将能够 makeObservable 处理您在运行时传递给它的任何类型。 <T> 将根据传递给方法的参数进行解析。

想要制作一个处理 DateObservable 吗? makeObservable(new Callable<Date>() {...}); StringObservablemakeObservable(new Callable<String>() {...});

请注意,由于您的代码的可观察流尚未转换输入数据,因此它也会输出一个 Observable<T>。另请注意,您的代码无法提前知道它可以调用的方法,因此它可以应用的可能转换将受到限制。

其中一个转换(又名 map)可能是获取 T 对象的字符串表示形式。

顺便说一下,是的,你在 map 函数中是正确的,T 是输入类型,R 是输出类型,所以在你的代码中应用这样的映射将导致 makeObservable 的 return 类型为 Observable<R>(将 R 替换为具体类型,您不能在此处泛化)。

public static <T> Observable<String> makeToStringObservable(final Callable<T> func) {
    return Observable.create(
            new Observable.OnSubscribe<T>() {
                @Override
                public void call(Subscriber<? super T> subscriber) {
                    try {
                        T observed = func.call();
                        if (observed != null) { // to make defaultIfEmpty work
                            subscriber.onNext(observed);
                        }
                        subscriber.onCompleted();
                    } catch (Exception ex) {
                        subscriber.onError(ex);
                    }
                }
            }).map(new Func1<T, String>() {
                @Override
                public String call(T t) {
                  //toString() is a method that is always there, yay
                  return t.toString();
            }
    });