码迷,mamicode.com
首页 > 其他好文 > 详细

继承—Car

时间:2016-05-23 14:55:13      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

编写一个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     }

运行结果:

技术分享

继承—Car

标签:

原文地址:http://www.cnblogs.com/ouyangtangfeng99/p/5519771.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!