【问题标题】:How to use toString in this example?在这个例子中如何使用 toString?
【发布时间】:2019-12-06 07:53:22
【问题描述】:

目前正在做一些功课并遇到麻烦 - 我不确定如何正确使用 toString。这是作业问题:

创建一个名为 Vehicle 的类,作为车辆类型的超类。 Vehicle 类包含用于车轮数量和每加仑平均英里数的私有变量。 Vehicle 类还包含一个构造函数,其带有用于轮数和每加仑平均英里数的整数参数,以及一个 display() 方法,该方法使用 toString() 方法将整数值转换为 String 来打印所需的输出。

创建两个子类,Car 和 MotorCycle,它们扩展了 Vehicle 类。 每个子类都包含一个构造函数,该构造函数接受英里/加仑值作为参数,并将车轮数强制为适当的值——摩托车为 2,汽车为 4。使用超类构造函数设置wheels和mpg数据字段(使用super关键字)。

编写一个 UseVehicle 类来实例化每个子类的一个对象并显示该对象的值。 将文件保存为 Vehicle.java、Car.java、MotorCycle.java 和 UseVehicle.java

public class Vehicle {

    protected int wheelnumber;
    protected int mpg;

    Vehicle (int wheelnum, int aMpg) {

    wheelnumber = wheelnum;
    mpg = aMpg;

    }

    public void display() {
    System.out.println("Wheels: " + toString(wheelnumber) + " Mpg: " + toString(mpg));

    }

}

public class Car extends Vehicle{

    Car (int CarMPG) {

        super(4, CarMPG);
        }
}

public class Motorcycle extends Vehicle{

    Motorcycle (int MotorcycleMPG) {

        super(2, MotorcycleMPG);

    }

}
public class UseVehicle {

    public static void main(String[] args) {
        Car Car1 = new Car(30);
        Motorcycle Motorcycle2 = new Motorcycle(60);

        System.out.print("Car--> ");
        Car1.display();
        System.out.print("Motorcycle—> ");
        Motorcycle2.display();

    }

}

【问题讨论】:

  • Java 中没有这样的 global 方法方法。在你的代码中,所有需要的是System.out.println("Wheels: " + wheelnumber + " Mpg: " + mpg);
  • 为什么不重写对象的tostring方法?
  • 为什么需要 toString 来打印原语?

标签: java tostring


【解决方案1】:

这里有几个问题。 一,你的问题说wheelnumber和mpg应该是私有的。 其次,数据类型应该是整数。希望以下内容能解决您的问题。

public class Vehicle {

  //should be private as per question
  private Integer wheelnumber;
  private Integer mpg;

  Vehicle (int wheelnum, int aMpg) {
    wheelnumber = wheelnum;
    mpg = aMpg;
  }

  public void display() {
    System.out.println("Wheels: " + wheelnumber.toString + " Mpg: " + mpg.toString);
  }

}

现在,详细说明一下,方法toString() 是在Java Object 类中定义的,您默认定义的任何类都会扩展它。

您使用的数据类型 - “int”是 primitive data type(直接取自 C - 简单但不是面向对象的方法),它不扩展 Java Object 类。因此,您不能将 toString() 方法用于原始数据类型的任何实例。

而“Integer”数据类型是在 Java API 中定义的类,它扩展了 Object 类并在其中定义了 toString() 方法。因此,您可以对此类的任何实例使用 toString() 方法。

话虽如此,如果您绝对必须将“int”变量转换为字符串,我建议您执行以下操作:

int i = 10;
System.out.println("Value of i: " + Integer.toString(i));

请注意,int 或 Integer 无需调用 toString() 方法即可轻松转换为字符串,因为当您将其与 String 连接时,Java 会自动为您执行此操作 - 如下所示:

int p = 10;
Integer q = new Integer(20); 
System.out.println("value of p:" + p + ", q:" + q);

但是,这不是传统的方式,我猜这不是你的老师想要的。

【讨论】:

    【解决方案2】:

    使用 toString() 方法打印所需的输出以将整数值转换为字符串。

    在这句话中,老师指的是Integer.toString(),见Integer类。可能,the overload Integer.toString(int i)

    alternatives of converting an integer to a string,其中一些看起来更简单。但是老师可能有理由要求这种特殊的方法。

    【讨论】:

      【解决方案3】:

      我会在 Vehical 上实现 toString()

      public class Vehicle {
      
          protected int wheelnumber;
          protected int mpg;
      
          Vehicle (int wheelnum, int aMpg) {
              this.wheelnumber = wheelnum;
              this.mpg = aMpg;
          }
      
          public void display() {
              System.out.println(this.toString());
          }
      
          @Override
          public String toString() {
              return "Wheels: " + this.wheelnumber + " Mpg: " + this.mpg;
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多