简体   繁体   English

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

[英]How do I set limited generic return type

Taking the following 2 objects, I cant figure out how to make the following work. 以以下两个对象为例,我不知道如何进行以下工作。

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

First the class I am working with 首先是我正在上课的班级

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(){

    }

}

And then enum ResultType 然后枚举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);
    }
}

What I would like to do is have a way to check what mType is (String, Boolean, or Number) and then return that actual object. 我想做的是有一种方法来检查什么是mType(字符串,布尔值或数字),然后返回该实际对象。 Something like, 就像是,

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

Though obviously I would have actual information to pass back instead. 虽然显然我会传回实际信息。

But I dont know if that is possible, and if so, I don't know how I would go about doing it. 但是我不知道这是否可能,如果可以,我不知道该怎么做。 It does appear that Android is able to do it via AsyncTask . 看来Android可以通过AsyncTask做到这一点。

For reference, I found most of this from This Question 作为参考,我从This Question中找到了大部分

I would suggest you do it like this, which doesn't convert the info values to String, ie mInfo is Object , not String . 我建议您这样做,它不会将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;
    }

}

Then you use it like this: 然后像这样使用它:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM