【问题标题】:Java Return between a int or String(unknown return type)Java 在 int 或 String 之间返回(未知返回类型)
【发布时间】:2014-03-19 15:42:28
【问题描述】:

我的程序从文本文件加载信息并创建一个对象数组,其中包含整数或字符串的信息。

然后我希望对象返回字符串或整数,具体取决于对象是保存整数值还是字符串值。

编辑... 所以这是我的类型类,如果文本文件中的字段是数字,则保存一个 int;如果字段是单词,则保存一个字符串,并且保存在 Type 数组中。

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,我想在我的主类中返回没有任何长语句的数据类型。

【问题讨论】:

  • 到目前为止你尝试了什么?
  • 我们想为您提供帮助,但如果没有具体问题,我们将无能为力。请发布您的代码(工作与否),并指定确切的问题。

标签: java string object int return


【解决方案1】:

如果您只想获取特定的值,您可以在 Type 类中添加一个方法并从该方法中获取值,虽然丑陋但可以满足您的要求:

public <T> T getDynamicValue(Type t) {
     if (isInt()) {
          return (T) ((Integer) t.getValue());
     } else {
          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));
}

如果您想对这些数据进行一些操作,您必须进行instanceof 检查并对其进行强制转换,例如使用名称值进行一些拆分(或字符串方法)...

【讨论】:

    【解决方案2】:

    您无法选择在运行时返回的对象类型。您唯一的选择是返回Object。您可以使用此代码检查它是String 还是int,例如:

    if(object instanceof String) {
       //... it's a string
    }
    else {
       //...otherwise it's an int
    }
    

    【讨论】:

      【解决方案3】:

      如果您将所有输入读入 String 实例,则需要针对 Integer.parseString(value) 测试这些值,以确定它是否实际上是一个 Integer。

      【讨论】:

        【解决方案4】:

        您可以尝试将对象转换为Integer 并捕获ClassCastException

        try {
            int i = (Integer) object;
        }
        catch (ClassCastException e){
            String s = (String) object;
        }
        

        【讨论】:

        • 我认为使用instanceof而不是捕获异常会更好
        【解决方案5】:

        当我遇到这类问题时,我有时会通过扭转问题并使用回调式解决方案来解决它。

        例如:

        for ( Type t : array ) {
          t.process( callback );
        }
        

        回调如下所示:

        interface Callback {
          public void processInt(....);
          public void processString(....);
        }
        

        然后您可以使用if (isInt()) callback.processInt() else callback.processString() 实现流程方法,或者如果您更改Type 的定义,您可以使用继承树为您完成。

        例如:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-08-25
          • 2013-01-05
          • 2016-10-28
          • 1970-01-01
          • 1970-01-01
          • 2019-10-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多