在对象数组中使用父 class toString 打印子 class JAVA 的父 class 变量
Using parent class toString in object Array to print parent class variables for child class JAVA
我正在尝试编写一个代码,用于获取车辆名称等并将它们存储在一个对象数组中。我有 Vehicle 作为父级 class,带有打印名称、品牌和加仑油箱的 toString,父级的两个子 class 是公共汽车和汽车。 Car and Bus 问有多少门、轮子或乘客。在随机化顺序后打印数组时,当数组的索引是父级 class 时,我只从父级 class 获取要打印的字符串。我要打印的是:
车辆 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";
}
}
在您的代码中,您正在覆盖子 classes 中的 toString
方法。虽然我的理解是您还想打印父项和子项 class 的值。所以你的 toString
in child class 应该像下面这样,你首先调用父 toString
也。
@Override
public String toString() {
return super.toString() + "wheels " wheels + " doors " + numOfdoors ; // or something like this
}
我正在尝试编写一个代码,用于获取车辆名称等并将它们存储在一个对象数组中。我有 Vehicle 作为父级 class,带有打印名称、品牌和加仑油箱的 toString,父级的两个子 class 是公共汽车和汽车。 Car and Bus 问有多少门、轮子或乘客。在随机化顺序后打印数组时,当数组的索引是父级 class 时,我只从父级 class 获取要打印的字符串。我要打印的是: 车辆 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";
}
}
在您的代码中,您正在覆盖子 classes 中的 toString
方法。虽然我的理解是您还想打印父项和子项 class 的值。所以你的 toString
in child class 应该像下面这样,你首先调用父 toString
也。
@Override
public String toString() {
return super.toString() + "wheels " wheels + " doors " + numOfdoors ; // or something like this
}