int或字符串之间的Java返回(未知返回类型)
我的程序从文本文件加载信息,并创建一个对象数组,其中包含的信息是整数还是字符串 然后,我希望该对象返回字符串或整数,这取决于该对象是持有整数值还是字符串值 编辑。。。 所以这里是我的类型类,如果文本文件中的字段是数字,它保存int;如果字段是单词,它保存在类型数组中,它保存字符串int或字符串之间的Java返回(未知返回类型),java,string,object,int,return,Java,String,Object,Int,Return,我的程序从文本文件加载信息,并创建一个对象数组,其中包含的信息是整数还是字符串 然后,我希望该对象返回字符串或整数,这取决于该对象是持有整数值还是字符串值 编辑。。。 所以这里是我的类型类,如果文本文件中的字段是数字,它保存int;如果字段是单词,它保存在类型数组中,它保存字符串 public class Type { private String name; private int value; public Type(String name) { this.nam
public class Type {
private String name;
private int value;
public Type(String name) {
this.name = name;
}
public Type(int value) {
this.value = value;
}
public String getName() {
return this.name;
}
public int getValue() {
return this.value;
}
public boolean isInt() {
boolean isInt = false;
if (this.value != 0) {
isInt = true;
return isInt;
}
return isInt;
}
}
因此,在我的数组中,可以是Int或String,我希望返回的数据类型在我的主类中没有任何长语句。您不能选择在运行时返回的对象类型。您唯一的选择是返回一个
对象
。您可以使用此代码检查它是字符串
还是int
,例如:
if(object instanceof String) {
//... it's a string
}
else {
//...otherwise it's an int
}
for ( Type t : array ) {
t.process( callback );
}
interface Type {
public void process( Callback cb );
}
class IntType implements Type {
public void process( Callback cb ) {
cb.processInt(...);
}
}
class StringType implements Type {
public void process( Callback cb ) {
cb.processString(...);
}
}
如果要将所有输入读取到字符串实例中,则需要根据Integer.parseString(value)测试值,以确定它实际上是否为整数。您可以尝试将对象强制转换为
整数
,并捕获ClassCastException
:
try {
int i = (Integer) object;
}
catch (ClassCastException e){
String s = (String) object;
}
如果您严格地只想获取特定的值,您可以向
类型
类中添加一个方法,并从该方法中获取值,但这是您想要的:
public <T> T getDynamicValue(Type t) {
if (isInt()) {
return (T) ((Integer) t.getValue());
} else {
return (T) t.getName();
}
}
public T getDynamicValue(类型T){
if(isInt()){
返回(T)((整数)T.getValue());
}否则{
return(T)T.getName();
}
}
使用资讯科技:
List<Type> dynamicList = Arrays.asList(new Type[]{new Type(1), new Type(2), new Type("dog")});
for (Type t : dynamicList) {
System.out.println("T -> " + t.getDynamicValue(t));
}
List dynamicList=Arrays.asList(新类型[]{new Type(1)、新类型(2)、新类型(“dog”)});
用于(类型t:dynamicList){
System.out.println(“T->”+T.getDynamicValue(T));
}
如果你想对这些数据执行一些操作,你必须做一个
instanceof
检查并强制转换它,例如一些名称值为的拆分(或字符串方法…当我遇到这种类型的问题时,我有时会通过扭转问题并使用回调式解决方案来解决它
例如:
if(object instanceof String) {
//... it's a string
}
else {
//...otherwise it's an int
}
for ( Type t : array ) {
t.process( callback );
}
interface Type {
public void process( Callback cb );
}
class IntType implements Type {
public void process( Callback cb ) {
cb.processInt(...);
}
}
class StringType implements Type {
public void process( Callback cb ) {
cb.processString(...);
}
}
其中回调如下所示:
interface Callback {
public void processInt(....);
public void processString(....);
}
然后,您可以使用if(isInt())回调.processInt()else callback.processString()
,或者如果更改类型的定义,您可以使用继承树来实现该过程方法
例如:
if(object instanceof String) {
//... it's a string
}
else {
//...otherwise it's an int
}
for ( Type t : array ) {
t.process( callback );
}
interface Type {
public void process( Callback cb );
}
class IntType implements Type {
public void process( Callback cb ) {
cb.processInt(...);
}
}
class StringType implements Type {
public void process( Callback cb ) {
cb.processString(...);
}
}
到目前为止你做了什么?我们想帮助你,但没有具体问题,我们什么也做不了。请发布您的代码(工作或不工作),并指定确切的问题。我认为最好使用instanceof
而不是捕获异常