编写一个Car类,具有final类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。

package b;

public class Car {
      public final static Car pinpai=new Car();
      public static Car instance()
      {
        return pinpai;          
      }
      public void drive()
      {
          System.out.println("请安全驾驶!");
      }
}
package b;

public class Aodi extends Car {
    private String price;
    private String xinghao;
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getXinghao() {
        return xinghao;
    }
    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }
    public void biansu(int a)
    {
        if(a>0)
        {
            System.out.println("加速");
        }
        else if(a<0)
        {
            System.out.println("减速");
        }
        else
        {
            System.out.println("匀速前进");
        }
        
    }

}
package b;

public class Benchi extends Car {
    private String price;
    private String xinghao;
    public String getPrice() {
        return price;
    }
    public void setPrice(String price) {
        this.price = price;
    }
    public String getXinghao() {
        return xinghao;
    }
    public void setXinghao(String xinghao) {
        this.xinghao = xinghao;
    }
    public void biansu(int a)
    {
        if(a>0)
        {
            System.out.println("加速");
        }
        else if(a<0)
        {
            System.out.println("减速");
        }
        else
        {
            System.out.println("匀速前进");
        }
        
    }
}
package b;

public class E {

    public static void main(String[] args) {
        Aodi aodi=new Aodi();
        aodi.setXinghao("奥迪Q7");
        aodi.setPrice("110万");
        System.out.println(aodi.getXinghao());
        System.out.println(aodi.getPrice());
        aodi.biansu(90);
        aodi.drive();
        Benchi benchi=new Benchi();
        benchi.setXinghao("迈巴赫");
        benchi.setPrice("200万");
        System.out.println(benchi.getXinghao());
        System.out.println(benchi.getPrice());
        benchi.biansu(-90);
        benchi.drive();
    }

}

posted @ 2016-05-23 14:34  凌零聆  阅读(4367)  评论(0编辑  收藏  举报