【问题标题】:ToString and Output issuesToString 和输出问题
【发布时间】:2015-12-06 03:06:46
【问题描述】:

我的程序需要一些帮助。

这是我的任务

我已经编写了代码,但是当我要打印我的代码时,它只打印出 Int。 (注意我的格式好多了 — 我不习惯这个网站)。

这是我的类文件:

public class Card
{

  //instance variables
  public String rank;
  public String suit ;
  public int cardValue;

  //constructor

 public Card(String rank, String suit, int cardValue)
 {
  this.rank = rank;  
  this.suit = suit;
  this.cardValue = cardValue; 
 }

 //accessors
 //get methods

 public String getrank()   
 {     
 return rank;   
 }

 public String getsuit()
 {
 return suit; 
 }

 public int getcardValue() 
 {
 return cardValue;  
 }

 //toString
 public String toString()
  {
    String output = 
    "The" + rank + "of" + suit + "=" + cardValue + "\n";
    return output;
  }
}

这是我的跑步者文件:

import static java.lang.System.*;

public class CardRunner
{
 public static void main( String args[] )
 {

 Card bob = new Card("ACE", "SPADES", 11); 
 System.out.println(bob.cardValue); 
 }
}

【问题讨论】:

  • 类中的字段(当典型的类实现 getter 时)应该是私有的,然后编译器将在您的第一个循环中给出错误。在 toString() 中删除 "\n" ,在外部循环中使用 "\n"
  • 为什么要为这么平庸的问题投票?我同意,当然不是“下降”。恭喜autor给了你第一次尝试,不仅是“给我写代码”,但是这对于“up”来说太低了

标签: java variables int tostring


【解决方案1】:

如果您使用System.out.println(bob.cardValue);,那么它会打印单个对象属性。 如果要打印完整的对象属性,请传递完整的对象而不是传递单个属性。

打印这个:-"System.out.println(bob);" 您还需要覆盖toString() 函数。

 //toString
 public String toString()
  {
    String output = 
    "The" + rank + "of" + suit + "=" + cardValue + "\n";
    return output;
  }

【讨论】:

    【解决方案2】:

    正如其他人所写的那样——当你写 System.out.println(bob.cardValue); 时,它打印的正是那个参数。

    你需要调用你重写的“toString()method(顺便说一句,每个对象都有它,每个类,java中的每个对象都是“Object的孩子” " 类)

    对于调用 toString(),您不需要打印某个类的实例 (bob.cardValue) 的某些参数,而是打印该实例(对象)作为它 (bob) bob 是对象,“Card”类的实例:))。

    toString() 使用的一些示例:

    System.out.println(bob);  //in fact, will be called toString() also
    System.out.println(bob.toString());
    
    //or if you just need save instance in your custom string format in some *String* variable
    String bobInMyString = bob.toString();
    String bobInMyStringUsingCasttoString = (String) bob; //should also call toString()
    

    顺便说一句,它应该看起来像(编译器的注释):

    @Override
     public String toString()
      {
        String output = 
        "The" + rank + "of" + suit + "=" + cardValue + "\n";
        return output;
      }
    }
    

    如果您问为什么要使用 toString() 覆盖,请查看例如。在that very good example(复数)上,对于自定义类或将对象写入文件等很有用。

    【讨论】:

      【解决方案3】:

      如果你想从 bob 输出所有数据,你应该这样做:

      System.out.println(bob.toString());
      

      【讨论】:

        【解决方案4】:

        试试这个

         System.out.println(bob); 
        

        【讨论】:

          【解决方案5】:

          当您调用System.out.println(bob.cardValue); 时,您只打印您的CardcardValue 属性。

          您应该打印Card 实例以便执行您的toString 方法:

          System.out.println(bob); 
          

          【讨论】:

          • 不是属性而是字段,不正确地公开,这是可能的
          猜你喜欢
          • 2012-01-29
          • 1970-01-01
          • 1970-01-01
          • 2014-05-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多