gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 10:52:09 26 4
gpt4 key购买 nike

采用以下 2 个对象,我无法弄清楚如何进行以下工作。

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

首先是我正在使用的类(class)

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 是什么(字符串、 boolean 值或数字),然后返回该实际对象。类似这样的东西,

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 找到了大部分内容

最佳答案

我建议你这样做,它不会将 info 值转换为字符串,即 mInfoObject,而不是字符串

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

关于java - 如何设置有限的通用返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52086954/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com