Java层次结构和对象数组
我有很多关于这段代码的问题,这段代码是我为一个学校项目写的。我正在尝试这样做: 创建并实现一个类层次结构,其中Vehicle超类、“Motorcycle”和“Truck”是子类Java层次结构和对象数组,java,arrays,oop,hierarchy,Java,Arrays,Oop,Hierarchy,我有很多关于这段代码的问题,这段代码是我为一个学校项目写的。我正在尝试这样做: 创建并实现一个类层次结构,其中Vehicle超类、“Motorcycle”和“Truck”是子类 摩托车和卡车共有的变量是车轮和重量以及方法显示()。属性不应在类外部可访问。display()将打印轮子和重量,并且应该仅对公共类和所有子类都可以访问 摩托车等级特有的数据应为乘客。卡车类别特有的数据应为有效载荷。包括显示此信息的方法 绘制类层次结构 创建一个“驱动程序”类来测试您的层次结构。允许用户通过在车辆阵列中存
- 摩托车和卡车共有的变量是车轮和重量以及方法显示()。属性不应在类外部可访问。display()将打印轮子和重量,并且应该仅对公共类和所有子类都可以访问
- 摩托车等级特有的数据应为乘客。卡车类别特有的数据应为有效载荷。包括显示此信息的方法
- 绘制类层次结构
- 创建一个“驱动程序”类来测试您的层次结构。允许用户通过在车辆阵列中存储参考来创建多个对象。您应该允许用户通过输入“m”来创建摩托车对象,然后请求相关属性。沿相同的直线,使用“t”创建卡车对象
- 输入所有数据后,通过调用display()方法打印出每个对象的内容。如果是摩托车,则打印乘客人数;如果是卡车,则打印有效载荷。注意,display()方法必须为不同的子类重写
import java.util.Scanner;
class Hierarchy{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
Vehicle [] garage = new Vehicle [3];
for(int i = 0; i< garage.length;i++){
System.out.println("Please enter 't' to create a truck, and 'm' for a motorcycle");
String charIn = in.nextLine();
if(charIn == "m"){
System.out.println("What is the payload of the Truck");
int payload = in.nextInt();
System.out.println("What is the weight of the Truck");
int weight = in.nextInt();
System.out.println("How many wheels does the Truck have");
int wheels = in.nextInt();
garage[i] = new Truck(payload, wheels, weight);
}else{
System.out.println("How many passengers can the Motorcycle seat");
int passengers = in.nextInt();
System.out.println("What is the weight of the Motorcycle");
int weight = in.nextInt();
System.out.println("How many wheels does the Motorcycle have");
int wheels = in.nextInt();
garage[i] = new Motorcycle(passengers, weight, wheels);
}
}
//display();
}
}
class Vehicle{
int wheels;
int weight;
public Vehicle(int wheels, int weight){
weight = this.weight;
wheels = this.wheels;
}
//private void display(int weight, int passengers, int wheels){
//}
//private void display(int weight, int payload, int wheels){
//}
}
class Truck extends Vehicle{
int payload;
public int getWheels(){
return wheels;
}
public int getWeight(){
return weight;
}
public Truck(int wheels, int payload, int weight){
super(wheels, weight);
this.payload = payload;
}
}
class Motorcycle extends Vehicle{
int passengers;
public int getWheels(){
return wheels;
}
public int getWeight(){
return weight;
}
public Motorcycle(int wheels, int passengers, int weight){
super(wheels, weight);
this.passengers = passengers;
}
}
import java.util.Scanner;
类层次结构{
公共静态void main(字符串[]args){
扫描仪输入=新扫描仪(系统输入);
车辆[]车库=新车[3];
对于(int i=0;i
我尝试了尽可能多地查找资源,但是我看不到一种方法可以将数据输入到超类和子类中,然后将信息放入一个对象(车库数组)。还有,谁能给我一些方法来打印车库阵列对象中的数据?当然,如果你看到任何愚蠢的错误,请随时告诉我。
非常感谢你的帮助 从以下内容开始:
class Vehicle {
int wheels;
int weight;
public Vehicle(int wheels, int weight){
this.weight = weight;
this.wheels = wheels;
}
public int getWheels() {
return wheels;
}
public int getWeight() {
return weight;
}
}
class Truck extends Vehicle {
int payload;
public Truck(int wheels, int payload, int weight){
super(wheels, weight);
this.payload = payload;
}
public String toString() {
// todo - write code to output the truck details as a string
return "truck details here";
}
}
class Motorcycle extends Vehicle{
int passengers;
public Motorcycle(int wheels, int passengers, int weight){
super(wheels, weight);
this.passengers = passengers;
}
public String toString() {
// todo - write code to output the motorcycle details as a string
return "motorcycle details here";
}
}
创建卡车和摩托车并将它们放入车辆数组的代码看起来很好
当您想要打印它们时,只需在每个对象上循环调用toString的数组并打印出来:
for(Vehicle vehicle : garage) {
System.out.println(vehicle.toString());
}
此外,您还可以测试charIn='m'是否创建了一辆卡车而不是摩托车。如果用户输入的不是
m
,那么您就创建了一个摩托车。为了很好地打印数组,arrays.toString(someArray)
是您的朋友。