zoukankan      html  css  js  c++  java
  • 定义一个名为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 }
  • 相关阅读:
    30网络通信之多线程
    U盘自动拷贝
    多态原理探究
    应用安全
    应用安全
    编码表/转义字符/进制转换
    代码审计
    文件上传
    渗透测试-Web安全-SSRF
    中间人攻击
  • 原文地址:https://www.cnblogs.com/a709898670/p/9370473.html
Copyright © 2011-2022 走看看