简体   繁体   中英

Why does the toString Method not work when it is overwritten?

public abstract class Car {
    // This class includes common properties for a car, in this way we wont have to change if we need to add a new car brand
        public String name;
        public String colour;
        public int model;
        public String feature;
        public String getFeature() {
            return feature;
        }
        public void setFeature(String feature) {
            this.feature = feature;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getColour() {
            return colour;
        }
        public void setColour(String colour) {
            this.colour = colour;
        }
        public int getModel() {
            return model;
        }
        public void setModel(int model) {
            this.model = model;
        }   
    }

Test.java

import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Scanner input  = new Scanner(System.in);
        CarFactory carfactory = new CarFactory();

        System.out.println("Hello, please enter your car brand \n BMW \n MERCEDE \n OPEL");
        Car usercar = null;


        String usertext = input.nextLine();
        usercar = carfactory.makeCar(usertext);
        System.out.println("enter colour of your car");
        usertext = input.nextLine();
        usercar.setColour(usertext);
        System.out.println("enter model of your car");
        usertext =input.nextLine();
        usercar.setModel(Integer.parseInt(usertext));

        System.out.println("Your Car Information;\n "+ usercar.getName()+" \n Colour:" + usercar.getColour() + "\n Model "+ usercar.getModel()+ "\n Your car's plus point is " + usercar.getFeature());

       }

The question is, if i want to print car information with toString Metod, how would it be? i wrote one in Car class but it didnt work, feature is assign from car's own classes..

here is the my toString metod

 public String toString(){
     return "Your Car Information;\n "+ getName()+" \n Colour:" + getColour() + "\n Model "+getModel()+ "\n Your car's plus point is " +getFeature();
 }

Firstly, you must override toString() method of java.lang.Object like this:

class Car {
    ...
    @Override
    public String toString() {
        return "Your Car Information;\n " + getName() + " \n Colour:" +
                getColour() + "\n Model " + getModel() + "\n Your car's plus point is " +
                getFeature();
    }
}

Secondly, you can use it like this:

public static void main(String[] args) {
    // 'CarClass' is a no-abstract class, who extends the 'Car' class
    Car car = new CarClass();
    // the first way
    String information = car.toString();
    System.out.println(information);
    // the second way
    System.out.println(car);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM