首页 > 代码库 > 作业2
作业2
编写一个Car类,具有String类型的属性品牌,具有功能drive;
定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特
性。
package Car;public class Car { private String pinpai; private double sudu; public String getPinpa() { return pinpai; } public void setPinpa(String pinpa) { this.pinpai = pinpa; } public double getSudu() { return sudu; } public void setSudu(double sudu) { this.sudu = sudu; } public String drive(){ return "汽车已启动!"; } } package Car;public class Aodi extends Car{ private double jiage; private String xinghao; public double getJiage() { return jiage; } public void setJiage(double jiage) { this.jiage = jiage; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public double biansu(){ double sudu=super.getSudu()+10; return sudu; }}package Car;public class Benchi extends Car{ private double jiage; private String xinghao; public double getJiage() { return jiage; } public void setJiage(double jiage) { this.jiage = jiage; } public String getXinghao() { return xinghao; } public void setXinghao(String xinghao) { this.xinghao = xinghao; } public double biansu(){ double sudu=super.getSudu()+10; return sudu; }}package Car;public class E { public static void main(String[] args){ Aodi aodi=new Aodi(); aodi.setJiage(666666); aodi.setXinghao("奥迪A6"); aodi.setSudu(50); System.out.println("奥迪车的型号是:"+aodi.getXinghao()+" 价格是:"+aodi.getJiage()); System.out.println(aodi.drive()); System.out.println("启动后的速度为:"+aodi.biansu()); Benchi benchi=new Benchi(); benchi.setJiage(888888); benchi.setXinghao("奔驰S600"); benchi.setSudu(45); System.out.println("奔驰车的型号是:"+benchi.getXinghao()+" 价格是:"+benchi.getJiage()); System.out.println(benchi.drive()); System.out.println("启动后的速度为:"+benchi.biansu()); }}
作业2
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。