从不同 类 打印 toString

Print toString from different classes

一段时间以来,我一直在努力解决这个问题,我搜索了各种问题,但我认为我离答案还很近。

我有一辆超级class 车辆,可以提供车辆的各种细节。然后我有我的 Car class 继承自 Vehicle.

我正在尝试从我的 Car class 打印 toString,它覆盖了 Vehicle class。

基本上我想打印出每辆车的信息。

这是我的主要内容

    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");

    }

}

这是我的车class

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;

        }

    }

我希望我的输出类似于 "The Ford is red, has 4 wheels and is a convertible",粗体文本分别来自 Vehicle 和 Car classes。

如何让粗体文本显示在输出中?现在我只得到默认值,例如 null、0 和 false。

感谢任何意见,我知道我可能正在做一些非常愚蠢的事情,这些事情非常明显,但我就是看不到。

谢谢。

I want my output to be something like "The Ford is red, has 4 wheels and is a convertible", with bold text coming from Vehicle and Car classes respectively.

如果你想包含这些词,那么你必须告诉它在你的 toString() 中包含这些词。

例如,如果你想让它打印"has 4 wheels",那么你不能只做getNumberOfWheels()。否则你只会得到 4。你必须做类似 "has " + getNumberOfWheels() + " wheels" 的事情。如果你不告诉它包含所有的单词,它就不会知道包含它们。

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);

但是 CarVehicle class 中的构造函数被创建,但没有为它们的局部变量赋值!因此 .toString() 没有得到任何显示!

所需的更改是:

主线 Class

System.out.println("The Ford is "+ Ford.toString() );

在车里Class

//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" ;
        }


    }

在车里Class

//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;

}