为什么返回类型不满足方法签名?

时间:2022-09-01 23:21:04

Why doesn't the return type satisfy the method signature in the following method?

为什么返回类型不满足以下方法中的方法签名?

protected Observable<List<? extends Person>> getLoadPersonsObservable() {
    return StudentsProvider.getStudentsProvider().getStudents().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

The observeOn() method returns the following:

observeOn()方法返回以下内容:

Observable<List<Student>>

And here is the Student class:

这是学生班:

public class Student extends Person {
    public Student(String name, String id, boolean approved) {
       super(name, id, approved);
    }
}

The error: 为什么返回类型不满足方法签名?

For now, getStudents() is a stub method, emulating a network call:

目前,getStudents()是一个存根方法,模拟网络调用:

 @Override
public Observable<List<Student>> getStudents() {

    final Observable<List<Student>> fetchStudents = Observable.create(new Observable.OnSubscribe<List<Student>>() {
        @Override
        public void call(Subscriber<? super List<Student>> subscriber) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            List <Student> stubList = new ArrayList<>();

            stubList.add(new Student("Freddie Mercury", "1", true));
            stubList.add(new Student("Jimmy Hendrix", "2", true));

            subscriber.onNext(stubList);

            subscriber.onCompleted();
        }
    });
    return fetchStudents;
}

Thanks in advance! - Mate

提前致谢! - 伴侣

2 个解决方案

#1


0  

First create a custom class for your Observer

首先为Observer创建一个自定义类

private class MyObservable<T extends Person> extends Observable<List<T>>{

}

then in your activity, add this method

然后在您的活动中添加此方法

private MyObservable<Student> getData(){
        return new MyObservable<>();
}

This is just an example of how you can achieve what you are trying.

这只是您如何实现目标的一个例子。

P.S. This might not be the final solution but it will definitely help you remove the static implementation like in your solution

附:这可能不是最终解决方案,但它肯定会帮助您删除解决方案中的静态实现

#2


0  

Ended up passing the Person subtype reference to the parent of the caller class and changing the abstract method signature as follows:

结束将Person子类型引用传递给调用者类的父级并更改抽象方法签名,如下所示:

 abstract protected Observable<List<P>> getLoadPersonsObservable();

Now the implementation of the method can look like this:

现在该方法的实现可能如下所示:

@Override
protected Observable<List<Student>> getLoadPersonsObservable() {
    return StudentsProvider.getStudentsProvider().getStudents().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

And voila, no errors. Thank you for your help everyone!

瞧,没有错误。谢谢大家的帮助!

#1


0  

First create a custom class for your Observer

首先为Observer创建一个自定义类

private class MyObservable<T extends Person> extends Observable<List<T>>{

}

then in your activity, add this method

然后在您的活动中添加此方法

private MyObservable<Student> getData(){
        return new MyObservable<>();
}

This is just an example of how you can achieve what you are trying.

这只是您如何实现目标的一个例子。

P.S. This might not be the final solution but it will definitely help you remove the static implementation like in your solution

附:这可能不是最终解决方案,但它肯定会帮助您删除解决方案中的静态实现

#2


0  

Ended up passing the Person subtype reference to the parent of the caller class and changing the abstract method signature as follows:

结束将Person子类型引用传递给调用者类的父级并更改抽象方法签名,如下所示:

 abstract protected Observable<List<P>> getLoadPersonsObservable();

Now the implementation of the method can look like this:

现在该方法的实现可能如下所示:

@Override
protected Observable<List<Student>> getLoadPersonsObservable() {
    return StudentsProvider.getStudentsProvider().getStudents().subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());
}

And voila, no errors. Thank you for your help everyone!

瞧,没有错误。谢谢大家的帮助!