【问题标题】:How to Print Results of Method in toString Method in the Same Class?如何在同一个类的 toString 方法中打印方法的结果?
【发布时间】:2016-12-03 22:01:31
【问题描述】:

我的班级中有这个方法叫Detective

  public boolean check (String line)
    {
        // Separte the information into three categories
        String[] categories = line.split("; ");
        String[] info;

        // Get the suspect's information
        info = categories[0].split(", ");
        suspect = new Person(info[0].charAt(0), Integer.parseInt(info[1]), Double.parseDouble(info[2]));

       Person person = new Person();
       //System.out.println(theOracle.checkPerson(suspect));
       if (theOracle.checkPerson(suspect) == 0){

           String st = null;
        categories[0] = st;
           String str = "";
           if (info[0].charAt(0) == 'M')
               str += "Male, ";
           else if (info[0].charAt(0) == 'F')
               str += "Female, ";
           else
               str += "unknown, ";

           str += Integer.parseInt(info[1]) + "'s, ";
           str += Double.parseDouble(info[2]) +"m";
           System.out.println(str);
            }
       // Get the location
       info = categories[1].split(", ");
       suspectLocation = new Location(Double.parseDouble(info[0]), Double.parseDouble(info[1]));
       //System.out.println(theOracle.checkLocation(suspectLocation));

       // Get the time
       info = categories[2].split(":");
       suspectTime = new Time(Integer.parseInt(info[0]), Integer.parseInt(info[1]));

        return false;
    }

和toString方法也在同一个类中:

public String toString()
{
    String str = "";
    str = "Person: " +   "/n";
    return str;
}

我怎样才能得到在/n之前在toString方法中打印的check方法中打印的结果?我试图复制我的检查方法中的内容并将其放入 toString 方法中,但它只是打印一个位置@哈希码。有没有办法简单地将检查方法的字符串结果复制到 toString 中以用于打印目的?

基本上我要问的是如何简单地获取在 check 方法中输出的 str 并在我的 toString 方法中打印它。

【问题讨论】:

  • 如果要打印出Person对象信息,请在Person类中实现toString()方法。
  • 这已经在 person 类中实现了 ` public String toString() { String str = ""; if (gender == 'F') str += "female,"; else if (gender == 'M') str += "male,";否则 str += "未知,"; str += ageRange + "'s, "; str += 高度 +"m";返回字符串; }`
  • 现在,如果我在侦探的 toString 中创建一个新的 person 对象并打印它会打印上面 ^^ 所说的内容,当它需要打印 M --> 男性转换等时。跨度>
  • 请编辑您的问题,而不是添加 cmets。评论很难格式化和阅读。
  • 如果你想让str 值超出它的方法,让它成为一个成员而不是一个局部变量。请注意,您的toString 取决于首先调用check,这至少可以说很奇怪,因为toString 应该始终为您提供可用的输出,无论对象处于什么状态。

标签: java methods tostring


【解决方案1】:

在实例级别取出str变量,而不是在check方法中声明它。

现在你的toString 方法:

public String toString()
{
    return str;
}

确保在您的应用程序调用toString 之前调用check,一种方法是从Detective 构造函数调用check 方法。

【讨论】:

  • 不能这样做。 str 将未声明。
  • 这是什么意思'未声明'。字符串 str = ""; //在实例级别
  • 你的意思是Private String str;
  • 是的,我可以这样做,但它会输出为 null。
  • 正如我在回答中提到的,您需要确保在调用toString之前调用check,可能来自Constructor?
【解决方案2】:

您需要更改方法check() 以返回一个字符串

public Strin gcheck (String line){...

在 check() 中删除或保留 print 调用:

System.out.println(str);

而不是 return false; 将其更改为 return str;

现在在toString() 中调用check() 并连接结果。

public String toString()
{
    String str = "";
    str = "Person: " +  check() + "/n";
    return str;
}

【讨论】:

    猜你喜欢
    • 2019-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-01
    • 1970-01-01
    相关资源
    最近更新 更多