如何设置有限的通用返回类型

时间:2018-08-29 22:31:33

标签: java android

通过以下两个对象,我不知道如何进行以下工作。

public final *Generic SubType* getInfo(){
...
}

首先是我正在上课的班级

public class ResultEntry<Type extends ResultType>{

    private final Type mType;
    private final String mLabel;
    private final String mInfo;

    private ResultEntry(final Type t, final String label, final String info){
        mType = t;
        mLabel = label;
        mInfo = info;
    }

    public static ResultEntry<ResultType> newInstance(final String label, final Number info){
        return new ResultEntry<>(ResultType.NUMBER, label, info.toString());
    }

    public static ResultEntry<ResultType> newInstance(final String label, final Boolean info){
        return new ResultEntry<>(ResultType.NUMBER, label, info.toString());
    }

    public static ResultEntry<ResultType> newInstance(final String label, final String info){
        return new ResultEntry<>(ResultType.NUMBER, label, info);
    }

    public final ResultType getType(){
        return mType;
    }

    public final String getLabel(){
        return mLabel;
    }

    public final *Generic SybType* getInfo(){

    }

}

然后枚举ResultType

public enum ResultType {

    STRING  ("STRING"),
    BOOLEAN ("BOOLEAN"),
    NUMBER  ("NUMBER");

    private final String s;

    ResultType(final String string){
        s = string;
    }

    public final boolean isString(){
        return s.equals(STRING.s);
    }

    public final boolean isBoolean(){
        return s.equals(BOOLEAN.s);
    }

    public final boolean isNumber(){
        return s.equals(NUMBER.s);
    }
}

我想做的是有一种方法来检查什么是mType(字符串,布尔值或数字),然后返回该实际对象。

public final *Generic SubType* getInfo(){
    if(mType.isString()) return new String();
    if(mType.isNumber()) return new Number();
    if(mType.isBoolean()) return new Boolean();
}

尽管很明显,我将获得实际的信息来代替。

但是我不知道这是否可行,如果可以,我不知道该怎么做。看来Android可以通过AsyncTask来做到这一点。

作为参考,我在This Question

中找到了大部分内容

1 个答案:

答案 0 :(得分:1)

我建议您这样做,这样不会将info的值转换为String,即mInfoObject,而不是String

public class ResultEntry<R> {

    private final ResultType mType;
    private final String mLabel;
    private final Object mInfo;

    private ResultEntry(final ResultType t, final String label, final Object info) {
        this.mType = t;
        this.mLabel = label;
        this.mInfo = info;
    }

    public static ResultEntry<Number> newInstance(final String label, final Number info) {
        return new ResultEntry<>(ResultType.NUMBER, label, info);
    }

    public static ResultEntry<Boolean> newInstance(final String label, final Boolean info) {
        return new ResultEntry<>(ResultType.BOOLEAN, label, info);
    }

    public static ResultEntry<String> newInstance(final String label, final String info) {
        return new ResultEntry<>(ResultType.STRING, label, info);
    }

    public final ResultType getType() {
        return this.mType;
    }

    public final String getLabel() {
        return this.mLabel;
    }

    @SuppressWarnings("unchecked")
    public final R getInfo() {
        return (R) this.mInfo;
    }

}

然后您像这样使用它:

ResultEntry<Number>  numEntry  = ResultEntry.newInstance("", 5);
ResultEntry<Boolean> boolEntry = ResultEntry.newInstance("", true);
ResultEntry<String>  strEntry  = ResultEntry.newInstance("", "Foo");

Number  numInfo  = numEntry.getInfo();
Boolean boolInfo = boolEntry.getInfo();
String  strInfo  = strEntry.getInfo();