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


     1 public class Vehicles {
     2     String brand;
     3     String color;
     4 public Vehicles(String brand,String color){
     5     this.brand=brand;
     6     this.color=color;
     7 }
     8 public void run(){
     9     System.out.println("我已经开动了");
    10 }
    11 public void showInfo(){
    12      System.out.println("商标: "  +  brand);
    13      System.out.println("颜色: "  +  color);
    14 }
    15 
    16 
    17 
    18 
    19 }
    20 --------------------------------------------------------------------------
    21 public class Car extends Vehicles{
    22     public Car c;
    23     private int seats;
    24     public Car(String brand, String color,int seats) {
    25         super(brand, color);
    26         this.seats=seats;
    27         // TODO Auto-generated constructor stub
    28     }
    29 public void showCar(){
    30     System.out.println("座位: "  + seats + " 个");
    31 }
    32 }
    33 --------------------------------------------------------------------------
    34 public class Truck extends Vehicles{
    35     private float load;
    36     public Truck(String brand, String color,float load) {
    37         
    38         super(brand, color);
    39         this.load=load;
    40         // TODO Auto-generated constructor stub
    41     }
    42     public void showTruck(){
    43         super.showInfo();
    44         System.out.println("载重"+load+"吨");
    45     }
    46 }
    47 --------------------------------------------------------------------------
    48 public class Test {
    49 
    50     public static void main(String[] args) {
    51         // TODO Auto-generated method stub
    52         Vehicles v=new Vehicles("奥迪", "黑色");
    53         v.showInfo();
    54         
    55         Car c= new Car("大众", "红色", 6);
    56         c.showCar();
    57         
    58         Truck truck = new Truck("解放", "蓝色", 10);
    59           truck.showTruck();
    60     }
    61 
    62     
    63 }
  • 相关阅读:
    (64)通信协议之一xml
    (63)通信协议之一json
    (61)C语言预处理命令详解
    (60) 结构体指针、结构体变量嵌套、结构体指针嵌套、函数指针、数组指针、指针数组、typedef 综合运用
    (59)Linux操作系统深入应用
    (58)PHP开发
    (57)Linux驱动开发之三Linux字符设备驱动
    (56)Linux驱动开发之二
    (55)Linux驱动开发之一驱动概述
    (54)LINUX应用编程和网络编程之九Linux网络通信实践
  • 原文地址:https://www.cnblogs.com/a709898670/p/9370473.html
Copyright © 2020-2023  润新知