• 继承—Car


    编写一个Car类,具有final类型的属性品牌,具有功能drive;

    定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;

    定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特性。

     1 public class Car {
     2     private final String PinPai;
     3     
     4     public String getPinPai(){
     5         return PinPai;
     6     }
     7     public void drive(){
     8         System.out.println("我可以行驶。");
     9     }
    10     
    11     public Car(String pinpai){
    12         PinPai=pinpai;
    13     }
    14     
    15 }
     1 public class Aodi extends Car {
     2 
     3     public double price;
     4     public String xinghao;
     5 
     6     public void biansu() {
     7         System.out.println("速度改变");
     8     }
     9 
    10     public Aodi() {
    11         super("奥迪");
    12     }
    13 
    14     public void Aodi(double price, String xinghao) {
    15         this.price = price;
    16         this.xinghao = xinghao;
    17     }
    18 
    19     @Override
    20     public String toString() {
    21         return "Aodi [PinPai=" + getPinPai() + ", price=" + price + ", xinghao=" + xinghao + "]";
    22     }
    23 
    24 }
     1 public class BenChi extends Car{
     2 
     3     public double price;
     4     public String xinghao;
     5     
     6     public void biansu(){
     7         System.out.println("速度改变");
     8     }
     9     
    10     public BenChi(){
    11         super("奔驰");
    12     }
    13     public void BenChi(double price, String xinghao){
    14         this.price=price;
    15         this.xinghao=xinghao;
    16     }
    17 
    18     @Override
    19     public String toString() {
    20         return "BenChi [PinPai=" + getPinPai() + ", price=" + price + ", xinghao=" + xinghao + "]";
    21     }
    22     
    23 }
     1     public static void main(String[] args) {
     2         Aodi ad = new Aodi();
     3         ad.Aodi(400000, "A6");
     4         ad.biansu();
     5         System.out.println(ad.toString());
     6 
     7         BenChi bc = new BenChi();
     8         System.out.println(bc.getPinPai());
     9         bc.BenChi(350000, "Benze S—500");
    10         System.out.println(bc.toString());
    11 
    12     }

    运行结果:

  • 相关阅读:
    学Java需要了解的linux系统和unix系统
    51Nod 1649: 齐头并进(最短路)
    51Nod 1694: 两条路径(图论)
    The Preliminary Contest for ICPC Asia Shanghai 2019
    牛客练习赛52 A:数数(快速幂)
    The Preliminary Contest for ICPC Asia Shenyang 2019
    The 2019 Asia Nanchang First Round Online Programming Contest
    2019 年百度之星·程序设计大赛
    2019中国大学生程序设计竞赛(CCPC)
    2019 年百度之星·程序设计大赛
  • 原文地址:https://www.cnblogs.com/ouyangtangfeng99/p/5519771.html
Copyright © 2020-2023  润新知