问一道初学者JAVA题,希望得到简单答案

2025-06-21 19:23:15
推荐回答(1个)
回答1:

1、交通工具实体类


public class Vehicles {

protected  String brand;

protected  String color;

public String getBrand() {
return brand;
}

public void setBrand(String brand) {
this.brand = brand;
}

public String getColor() {
return color;
}

public void setColor(String color) {
this.color = color;
}

//成员方法
public void run(){
System.out.println("我已经开动了");
}
public void showInfo(){
System.out.println("商标:"+this.brand);
System.out.println("颜色:"+this.color);
}

//构造方法
Vehicles(String brand,String color){
this.brand = brand;
this.color = color;
}
}

2、汽车实体类


public class Car extends Vehicles{

//构造方法
Car(String brand, String color,int seats) {
super(brand, color);
this.seats = seats;
// TODO Auto-generated constructor stub
}

private int seats;

public int getSeats() {
return seats;
}

public void setSeats(int seats) {
this.seats = seats;
}

//成员方法
public void showCar(){
System.out.println("商标:"+this.brand);
System.out.println("颜色:"+this.color);
System.out.println("座位:"+this.seats);
}
}

3、卡车实体类


public class Truck extends Vehicles{

Truck(String brand, String color ,float load) {
super(brand, color);
this.load = load;
// TODO Auto-generated constructor stub
}

private float load;

public float getLoad() {
return load;
}

public void setLoad(float load) {
this.load = load;
}

public void showTruck(){
System.out.println("商标:"+this.brand);
System.out.println("颜色:"+this.color);
System.out.println("载重:"+this.load);
}
}

4、测试类


public class TestMain {

public static void main(String[] args) {

Vehicles vehicles = new Vehicles("奔驰","黑色");
vehicles.run();
vehicles.showInfo();

Car car = new Car("奥迪","银色",5);
car.run();
car.showInfo();
car.showCar();

Truck truck = new Truck("东风","蓝色",(float) 18.9);
truck.run();
truck.showInfo();
truck.showTruck();
}
}