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

class Vehicle{
 int wheels;
 int weight;
 public void a1(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight);
 }
}
class car extends Vehicle{
 int wheels;
 int weight;
 int loader;
 public void a2(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight+" 有载人数:"+loader);
 }
}
class Truck extends car{
 int wheels;
 int weight;
 int loader;
 int payload;
 public void a3(){
  System.out.println("车轮数:"+wheels+" 车重:"+weight+" 有载人数:"+loader+" 车载重:"+payload);
 }
}
public class Test {
 public static void main(String[] args) {  
  Truck e=new Truck();
  e.loader=4;
  e.payload=30;
  e.weight=50;
  e.wheels=4;
  e.a3();
 }
}

 

 
posted on 2019-05-05 23:17  刘煜炀  阅读(130)  评论(0编辑  收藏  举报