Lesson_8 作业_1 car和truck

一.作业描述

  定义一个名为Vehicles(交通工具)的基类,该类中应包含String类型的成员属性brand(商标)和color(颜色),还应包含成员方法run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,在控制台显示商标和颜色),并编写构造方法初始化其成员属性。

  编写Car(小汽车)类继承于Vehicles类,增加int型成员属性seats(座位),还应增加成员方法showCar(在控制台显示小汽车的信息),并编写构造方法。

  编写Truck(卡车)类继承于Vehicles类,增加float型成员属性load(载重),还应增加成员方法showTruck(在控制台显示卡车的信息),并编写构造方法。

main方法中测试以上各类。

二.代码

 

  1 /************************************************************
  2 *                    Lesson_8 作业_1 -- car和truck
  3 *                         2013-01-18
  4 *                        by CocoonFan
  5 *
  6 *************************************************************
  7 *************************作业描述****************************
  8 *
  9 *    定义一个名为Vehicles(交通工具)的基类,该类中应包含String
 10 * 类型的成员属性brand(商标)和color(颜色),还应包含成员方法
 11 * run(行驶,在控制台显示“我已经开动了”)和showInfo(显示信息,
 12 * 在控制台显示商标和颜色),并编写构造方法初始化其成员属性。
 13 *    编写Car(小汽车)类继承于Vehicles类,增加int型成员属性
 14 * seats(座位),还应增加成员方法showCar(在控制台显示小汽车的
 15 * 信息),并编写构造方法。
 16 *    编写Truck(卡车)类继承于Vehicles类,增加float型成员属性
 17 * load(载重),还应增加成员方法showTruck(在控制台显示卡车的
 18 * 信息),并编写构造方法。
 19 *    在main方法中测试以上各类。
 20 *************************************************************/
 21 public class TestVehicles {
 22     public static void main(String[] args) {
 23         Car car = new Car("宝马", "红色", 6);
 24         Truck truck = new Truck("福田", "蓝色", 7.5f);
 25         car.run();
 26         car.showCar();
 27         truck.showTruck();
 28     }
 29 }
 30 
 31 class Vehicles{
 32     private String brand;
 33     private String color;
 34     
 35     public Vehicles(String brand, String color){
 36         this.brand = brand;
 37         this.color = color;
 38     }
 39     
 40     public String getBrand() {
 41         return brand;
 42     }
 43     public void setBrand(String brand) {
 44         this.brand = brand;
 45     }
 46     public String getColor() {
 47         return color;
 48     }
 49     public void setColorString(String color) {
 50         this.color = color;
 51     }
 52     
 53     public void run(){
 54         System.out.println("我已经开动了");
 55     }
 56     
 57     public void showInfo(){
 58         System.out.println("车的商标是:" + brand);
 59         System.out.println("车的颜色是:" + color);
 60     }    
 61 }
 62 
 63 class Car extends Vehicles{
 64     int seats;
 65     
 66     public Car(String brand, String color, int seats){
 67         super(brand, color);
 68         this.seats = seats;
 69     }
 70 
 71     public int getSeats() {
 72         return seats;
 73     }
 74 
 75     public void setSeats(int seats) {
 76         this.seats = seats;
 77     }
 78     
 79     public void showCar(){
 80         super.showInfo();
 81         System.out.println("座位数量是:" + this.seats);
 82         System.out.println();
 83     }
 84 }
 85 
 86 class Truck extends Vehicles{
 87     float load;
 88     
 89     public Truck(String brand, String color, float load){
 90         super(brand, color);
 91         this.load = load;
 92     }
 93 
 94     public float getLoad() {
 95         return load;
 96     }
 97     public void setLoad(float load) {
 98         this.load = load;
 99     }
100 
101 
102 
103     public void showTruck(){
104         super.showInfo();
105         System.out.println("车的载重量是:" + this.load + "t");
106         System.out.println();
107     }
108 }

 

三.运行结果

 

原文地址:https://www.cnblogs.com/CocoonFan/p/2867133.html