我已经尝试解决这个问题有一段时间了,我搜索了各种问题,但我认为我没有更接近。
我有一个父类(super class)车辆,它提供了车辆的各种详细信息。然后我有了继承自 Vehicle 的 Car 类。
我正在尝试从我的 Car 类打印 toString,该类覆盖了 Vehicle 类。
基本上我想打印出每辆车的信息。
这是我的主要内容
public static void main(String []args)
{
Car Ford = new Car(Red, 4, true);
//Ford = Ford.; Tried various things here to no avail, kept getting nullfalse in output
Car Honda = new Car(Blue, 4, false);
System.out.println(Ford.vehicleDetails);
System.out.println("Honda");
}
}
这是我的车辆类别
public class Vehicle
{
// Variables
private String vehicleColour;
private int numberOfWheels;
String vehicleDetails;
// Constructor
public Vehicle(String vehicleColour, int numberOfWheels)
{
}
// Getters
public String getVehicleColour()
{
return vehicleColour;
}
public int getNumberOfWheels()
{
return numberOfWheels;
}
// Setters
public void setVehicleColour(String vehicleColour)
{
this.vehicleColour = vehicleColour;
}
public void setNumberOfWheels(int numberOfWheels)
{
this.numberOfWheels = numberOfWheels;
}
// Constructor
public Vehicle ()
{
}
// toString method super
public String toString() {
String vehicleDetails = (getVehicleColour() + "" + getNumberOfWheels());
return vehicleDetails;
}
}
这是我的汽车类(class)
public class Car extends Vehicle
{
// Variables
private boolean convertible;
Car vehicleDetails;
// Getter
public boolean getConvertible()
{
return convertible;
}
// Setter
public void setConvertible(boolean convertible)
{
this.convertible = convertible;
}
// Constructor
public Car(String vehicleColour, int numberOfWheels, boolean convertible) {
}
// toString method override
@Override
public String toString() {
return super.toString() + convertible;
}
}
我希望我的输出类似于“福特是红色,有4轮子,是一辆敞篷车”,并带有粗体文本分别来自车辆和汽车类别。
如何让粗体文本显示在输出中?现在我只得到默认值,例如 null、0 和 false。
我很感激任何意见,我知道我可能正在做一些非常愚蠢的事情,这些事情非常明显,但我只是看不到它。
谢谢。
最佳答案
I want my output to be something like "The Ford is red, has 4 wheels and is a convertible"
对于您的上述要求,简单的解决方案:
System.out.println("The Ford is "+ Ford.toString() );
如果您打算从 Ford.toString
获取整个输出,那么在创建 Ford
对象时,您还需要传递汽车名称!如下所示,并对其构造函数进行必要的更改!
Car Ford = new Car("Ford","Red", 4, true);
然而,Car
和 Vehicle
类中的构造函数已创建,但没有为它们的局部变量赋值!因此 .toString()
没有显示任何内容!
所需的更改是:
在主类
System.out.println("The Ford is "+ Ford.toString() );
汽车类
//change in the constructor
public Car(String vehicleColour, int numberOfWheels, boolean convertible) {
setNumberOfWheels(numberOfWheels);
setVehicleColour(vehicleColour);
setConvertible(convertible);
}
I am trying to print the toString from my Car class, which overides the Vehicle class.
//change in toString()
@Override
public String toString() {
if(convertible){
return super.toString() +"and is a convertible" ;
}else{
return super.toString() +"and is not a convertible" ;
}
}
车辆类别
//change the constructor
public Vehicle(String vehicleColour, int numberOfWheels)
{
setNumberOfWheels(this.numberOfWheels);
setVehicleColour(this.vehicleColour);
}
Basically I want to print out the information about each car.
//change the toString()
public String toString() {
String vehicleDetails = (getVehicleColour() + ", has " +getNumberOfWheels()+" wheels");
return vehicleDetails;
}
关于java - 从不同的类打印 toString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42011067/