java - 为什么 toString 方法被覆盖后不起作用?

标签 java overriding

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

测试.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());

       }

问题是,如果我想用toString方法打印汽车信息,该怎么做?我在汽车类中写了一个,但它不起作用,功能是从汽车自己的类分配的..

这是我的 toString 方法

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

最佳答案

首先,您必须重写 java.lang.ObjecttoString() 方法,如下所示:

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

其次,你可以这样使用它:

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

关于java - 为什么 toString 方法被覆盖后不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31897066/

相关文章:

java - 嵌套 for 循环 - 有多少个?

java - 将 List 的 List 转换为另一个对象的 List

java - 在 JPanel 中的每个元素周围添加边框

java - getCipherSuite() 返回 SSL_NULL_WITH_NULL_NULL

java - 覆盖具有更高可见性的方法是好的做法吗?

c# - 使用反射动态覆盖 ToString()

java - SlpashScreen/性能: Proper way to initialize application data during startup especially using a SlpashScreen

java - 将 Java 功能与 Wikipedia 的多态性定义相关联

Java 在给定类数组时重载 toString() 方法

java - 方法不会覆盖其父类(super class)中的方法