문제

protected Response<T> parseNetworkResponse

is a method that returns a type of <T> , but I need <T> to be a List<T> or other kind of generic collection. This is because I am serializing objects from the network, and sometimes those objects become lists.

My objects are implementing required methods, so I can't just change the return type to List<T>; what can I do?

도움이 되었습니까?

해결책

Below is a sample, getErrors() and getWarnings() return List<T>

public interface ErrorHolder<T> {

    public void addError(T t);

    public int totalErrors();

    public boolean hasErrors();

    public void addWarning(T t);

    public int totalWarnings();

    public boolean hasWarnings();

    public List<T> getErrors();

    public List<T> getWarnings();
}

and in the implementing class -

private List<T> errors = new ArrayList<T>();

    public void addError(T t) {
        errors.add(t);

    }

public List<T> getErrors() {
        return errors;
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top