0


JAVA设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight

编写一个Java应用程序,设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight。小车类Car是Vehicle的子类,其中包含的属性有载人数loader。卡车类Truck是Car类的子类,其中包含的属性有载重量payload。每个类都有构造方法和输出相关数据的方法。最后,写一个测试类来测试这些类的功能。

class Vehicle{
private int wheel;
private double weight;

 public Vehicle() {

}
 public Vehicle(int wheel, double weight) {
     
     this.wheel = wheel;
     this.weight = weight;
 }
 public int getWheel() {
     return wheel;
 }
 public void setWheel(int wheel) {
     this.wheel = wheel;
 }
 public double getWeight() {
     return weight;
 }
 public void setWeight(double weight) {
     this.weight = weight;
 }
 public void getInfo () {
     System.out.println("车的轮子为:"+ wheel + ", 车重为:"+ weight);
 }

}
class Car extends Vehicle{
private int loader;

public int getLoader() {
     return loader;
 }

public void setLodar(int loader) {
     this.loader = loader;
 }
 public void  getInfo() {
     System.out.println("车的轮子为:"+ getWheel() + ", 车重为:"+ getWeight () +"载人数量为:" + loader);
 }

}
class Truck extends Car{
private double payload;

public double getPayload() {
     return payload;
 }

public void setPayload(double payload) {
     this.payload = payload;
 }
 public void getInfo () {
     System.out.println("车的轮子为:" + getWheel()+ ", 车重为:"+ getWeight () +"载人数量为:" + getLoader()+ ", 车的载重量为:" + payload);
 }

}
public class Demo3 {
public static void main(String[] args) {
Car car = new Car();
car.setWheel(4);
car.setWeight(23.6);
car.setLodar(12);
car.getInfo();
Truck truck = new Truck();
truck.setWheel(8);
truck.setWeight(45.7);
truck.setLodar(2);
truck.setPayload(50.6);
truck.getInfo();

}
}

标签: eclipse

本文转载自: https://blog.csdn.net/weixin_59545549/article/details/129562279
版权归原作者 邶羽 所有, 如有侵权,请联系我们删除。

“JAVA设计一个汽车类Vehicle,包含的属性有车轮个数wheels和车重weight”的评论:

还没有评论