【问题标题】:Using parent class toString in object Array to print parent class variables for child class JAVA使用对象数组中的父类toString为子类JAVA打印父类变量
【发布时间】:2021-11-16 05:59:53
【问题描述】:

我正在尝试编写一个代码,该代码采用车辆名称等并将它们存储在对象数组中。我有 Vehicle 作为父类,它有一个 toString 打印坦克的名称、品牌和加仑数,父类的两个子类是公共汽车和汽车。汽车和公共汽车询问有多少门、轮子或乘客。在随机化顺序后打印出数组时,我仅在数组的索引属于父类时从父类获取 to Strings 以打印出。我要打印的是: 车辆 0: 名称:汽车品牌:某物:坦克尺寸:15 门:4:轮子 4


我的代码如下:

import java.util.*;

public class Q1 {
    public static void main(String args[]){
        
        Scanner kb = new Scanner(System.in);

        Vehicle[] list = new Vehicle[3];

        for(int i =0;i < list.length;i++){
            int random = (int)(Math.random()*3);
            if(random == 0){
                System.out.println("What is the Name, brand and how big is the gas tank of the vehicle?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Vehicle a = new Vehicle(name,brand,gasTank);
                
                list[i] = a;
    
            }
            else if(random == 1){
                System.out.println("How many doors and wheels does the car have");
                int doors = kb.nextInt();
                int wheels = kb.nextInt();
                
                System.out.println("What is the Name, brand and how big is the gas tank of the car?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Car a = new Car(doors,wheels,name,brand,gasTank);
                list[i] = a;
            }
            else{
                System.out.println("How many doors and wheels does the bus have?");
                int doors = kb.nextInt();
                int wheels = kb.nextInt();
                
    
                System.out.println("What is the Name, brand and how big is the gas tank of the bus?");
                String name = kb.next();
                String brand = kb.next();
                int gasTank = kb.nextInt();
                
                Bus a = new Bus(doors,wheels,name,brand,gasTank);
    
                list[i] = a;
            }
            
        }
        printArray2(list);
        


    }
    public static void printArray(Vehicle[] list){
        for(int i = 0; i< list.length; i++){
            System.out.println("Vehicle "+ i);
            System.out.println(list[i]);
            System.out.println("___________");
        }
    }
    public static void printArray2(Vehicle[] list){
        for(int i = 0; i< list.length;i++){
            System.out.println("Vehicle "+ i);
            System.out.println(list[i].toString());
            System.out.println("___________");
        }
        
        
    }
}


class Vehicle {
    String name;
    String brand;
    int gasTank;

    public Vehicle(){
        this(" ", " ", 15);
    }
    public Vehicle(String name, String brand,int gasTank){
        this.name = name;
        this.brand = brand;
        this.gasTank = gasTank;

    }

    public String getname(){
        return name;
    }
    public String getbrand(){
        return brand;
    }  
    public int getgasTank(){
        return gasTank;
    } 
    public void setname(String name){
        this.name = name;
    }
    public void setbrand(String brand){
        this.brand = brand;
    }
    public void setgasTank(int gasTank){
        this.gasTank = gasTank;
    }
    
    public double milesPerGallon(double miles, double gallonsUsed){
        return  miles / gallonsUsed;
    }

    public String toString(){
        return " name: " + name + " Brand: " + brand + " Gallons in Tank: " + gasTank;
    }

    public double fillTank(double gallonsUsed){

        return (gasTank - gallonsUsed);
    }

}


class Car extends Vehicle{
    int numOfdoors;
    int numOfWheels;

    public Car(){
        this(2,4,"","",10);
    }
    public Car(int numOfDoors, int numOfWheels, String name, String brand, int gasTank){
        this.numOfdoors = numOfDoors;
        this.numOfWheels = numOfWheels;
        super.name = name;
        super.brand = brand;
        super.gasTank = gasTank;
    }
    public int getnumOfdoors(){
        return numOfdoors;
    }

    public int getnumOfWheels(){
        return numOfWheels;
    }
    public void setnumOfdoors(int numOfdoors){
        this.numOfdoors = numOfdoors;
    }
    public double milesPerGallon(double miles, double gallonsUsed){
        return miles / gallonsUsed;
    }

    public String toString(){
        return "The car has " + numOfWheels + " wheels and " + numOfdoors + " doors";
    }
}

class Bus extends Vehicle {
    int numOfWheels;
    int numOfPassengers;

    public Bus(){
        this(1,8,"","", 50);
    }
    public Bus(int numOfWheels, int numOfPassengers, String name, String brand, int gasTank){
        this.numOfWheels = numOfWheels;
        this.numOfPassengers = numOfPassengers;
        super.name = name;
        super.brand = brand;
        super.gasTank = gasTank;
    }

    public int getNumOfWheels(){
        return numOfWheels;
    }
    public int getNumOfPassengers(){
        return numOfPassengers;
    }

    public void setNumOfWheels(int numOfWheels){
        this.numOfWheels = numOfWheels;
    }
    public void setNumOfPassengers(int numOfPassengers){
        this.numOfPassengers = numOfPassengers;
    }
    public double milesPerGallon(double miles, double gallonsUsed){
        return miles / gallonsUsed;
    }
    public String toString(){
        return "The bus has "+ numOfWheels + " Wheels and " + numOfPassengers + " Passengers";
    }
}

【问题讨论】:

  • 如果你想保留父母的 toString 功能,你为什么要覆盖那个方法?只需在子类中删除它

标签: java class subclass tostring


【解决方案1】:

在您的代码中,您将覆盖子类中的toString 方法。虽然我的理解是你想打印父类的值以及子类。所以你在子类中的toString 应该如下所示,你首先调用父类toString

@Override
public String toString() {
 return super.toString() + "wheels " wheels + " doors " + numOfdoors ; // or something like this
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-21
    • 2012-07-04
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多