当前位置: 首页 > 知识库问答 >
问题:

使用对象数组中的父类toString打印子类JAVA的父类变量

欧阳昊焱
2023-03-14

我正在尝试编写一个代码,它采用车辆名称等并将它们存储在一个对象数组中。我有车辆作为父类,带有一个打印名称、品牌和加仑油箱的toString,父类的两个子类是bus和car。Car和Bus询问有多少门、轮子或乘客。当随机化顺序后打印数组时,我只从父类中获取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";
    }
}

共有1个答案

钱睿范
2023-03-14

代码中,您正在重写子类中的toString方法。我的理解是,您希望打印来自父类的值以及子类。因此,您在子类中的toString应该如下所示,您首先也调用parent。

@Override
public String toString() {
 return super.toString() + "wheels " wheels + " doors " + numOfdoors ; // or something like this
}
 类似资料:
  • 问题内容: 得知子类的类变量无法访问父类的类变量而没有特别指出父类的名称,我感到很惊讶: 为什么在定义By时我必须引用Ax,而不仅仅是x?这与我对实例变量的直觉是相反的,并且因为在定义B之后我可以引用Bx。 问题答案: 在Python中,在创建类之前,将在其自己的名称空间中执行类的主体(此后,该名称空间的成员将成为该类的成员)。因此,当解释器达到y = x + 1时,此时B类尚不存在,因此没有父类

  • 问题内容: 从子类对象调用该方法时,是否有任何优雅的方法可以使位于父类中的Java方法返回子类的对象? 我想在不使用其他接口和方法的情况下实现此功能,并在没有类强制转换,辅助参数等的情况下使用此功能。 更新: 抱歉,我不太清楚。 我想实现方法链,但是父类的方法存在问题:调用父类方法时,我无法访问子类方法… 我想我已经提出了我的想法的核心。 因此,方法应返回类的对象。 问题答案: 如果您只是在寻找针

  • 我有以下情况: 在执行代码时,输出为:5。 如何通过父类方法访问子类(B)变量(x)? 这是否可以在不重写print()方法的情况下完成(即在B中取消注释)? [这很重要,因为在重写时,我们将不得不再次重写print()方法的整个代码] 编辑过 进一步澄清:- 问题的动机是使用父类方法中的子类私有变量的值。这不需要更改父类私有变量的值来获得所需的结果 (感谢大家的时间和帮助)

  • 问题内容: 我有以下课程 这只是我实际架构的简化版本。最初,我不知道需要创建的人员类型,因此处理这些对象创建的函数将常规对象作为参数。 现在,我想使用此父类对象访问子类的方法。我还需要不时访问父类方法,所以 我不能 使其 抽象 。 我想我在上面的示例中简化太多了,所以这是实际的结构。 if语句显示“无法为QuestionOption找到getMultiple”。OuestionOption具有更多

  • 我对Java语言中的类型化有一个误解。问题是ClassCastException。例如,在这段代码中,假设动物是Dog类的父类,

  • 既然这个对象(在标题中说明)可以调用子类中的重写方法,为什么它不能调用子类的其他方法?我需要尽可能详细的回答,比如内存组织,JVM中的内部逻辑等。 下面的代码会让你清楚地理解我的问题。