这个例子中如何使用toString?
How to use toString in this example?
目前正在做一些作业并遇到了问题 - 我不确定如何正确使用 toString。这是作业题:
创建一个名为 class 的车辆作为车辆类型的超级class。
Vehicle class 包含车轮数量和每加仑平均英里数的私有变量。 Vehicle class 还包含一个带有整数参数的构造函数和一个 display() 方法,该方法使用 toString() 方法将整数值转换为 String 来打印所需的输出。
创建两个扩展 Vehicle class 的子class,Car 和 MotorCycle。
每个 subclass 包含一个构造函数,该构造函数接受每加仑英里数的值作为参数,并将车轮数强制为适当的值——摩托车为 2 个,汽车为 4 个。使用 superclass 构造函数设置 wheels 和 mpg 数据字段(使用 super 关键字)。
写一个 UseVehicle class 实例化每个 subclass 的一个对象并显示对象的值。
将文件另存为 Vehicle.java、Car.java、MotorCycle.java,然后使用 Vehicle.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();
}
}
prints the required output using the toString() method to convert the integer values to String.
这句话,老师指的是Integer.toString()
,见Integer
class. Likely, the overload Integer.toString(int i)
。
有alternatives of converting an integer to a string,其中一些看起来比较简单。但是老师要求这种特殊方法可能是有原因的。
我会在 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;
}
}
这里有几个问题。
一,你的问题说 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() is defined in Java Object class 以及您默认定义的任何 class 都会扩展它。
您使用的数据类型 - "int" 是 primitive data type (taken directly from C - simple but not object oriented approach) which does not extend Java Object class。因此,您不能对原始数据类型的任何实例使用 toString() 方法。
而“其中定义了 Integer" datatype is a class defined in Java API, it extends Object class and has toString() 方法。因此,您可以将 toString() 方法用于此 class 的任何实例。
话虽如此,如果您绝对必须将 "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);
但是,这不是传统的方式,我想这不是你的老师想要的这个作业。
目前正在做一些作业并遇到了问题 - 我不确定如何正确使用 toString。这是作业题:
创建一个名为 class 的车辆作为车辆类型的超级class。 Vehicle class 包含车轮数量和每加仑平均英里数的私有变量。 Vehicle class 还包含一个带有整数参数的构造函数和一个 display() 方法,该方法使用 toString() 方法将整数值转换为 String 来打印所需的输出。
创建两个扩展 Vehicle class 的子class,Car 和 MotorCycle。 每个 subclass 包含一个构造函数,该构造函数接受每加仑英里数的值作为参数,并将车轮数强制为适当的值——摩托车为 2 个,汽车为 4 个。使用 superclass 构造函数设置 wheels 和 mpg 数据字段(使用 super 关键字)。
写一个 UseVehicle class 实例化每个 subclass 的一个对象并显示对象的值。 将文件另存为 Vehicle.java、Car.java、MotorCycle.java,然后使用 Vehicle.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();
}
}
prints the required output using the toString() method to convert the integer values to String.
这句话,老师指的是Integer.toString()
,见Integer
class. Likely, the overload Integer.toString(int i)
。
有alternatives of converting an integer to a string,其中一些看起来比较简单。但是老师要求这种特殊方法可能是有原因的。
我会在 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;
}
}
这里有几个问题。 一,你的问题说 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() is defined in Java Object class 以及您默认定义的任何 class 都会扩展它。
您使用的数据类型 - "int" 是 primitive data type (taken directly from C - simple but not object oriented approach) which does not extend Java Object class。因此,您不能对原始数据类型的任何实例使用 toString() 方法。
而“其中定义了 Integer" datatype is a class defined in Java API, it extends Object class and has toString() 方法。因此,您可以将 toString() 方法用于此 class 的任何实例。
话虽如此,如果您绝对必须将 "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);
但是,这不是传统的方式,我想这不是你的老师想要的这个作业。