编写一个Car类,具有final类型的属性品牌,具有功能drive;
定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特
性。
package homework; public class Car { private final String pinpai=""; public void drive() { System.out.println("正在行驶"); } public String getPinpai() { return pinpai; } }
package homework; 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; } }
package homework; public class Benchi extends Car{ private double speed; public double getSpeed() { return speed; } public void setSpeed(double speed) { this.speed = speed; } public void changeSpeed(double speed) { for(int i=1;i<=20;i++) { speed+=10; if(speed>=150) { System.out.println("当前速度为:"+speed+"请减速慢行"); } } } }
package homework; public class E { public static void main(String[] args) { // TODO 自动生成的方法存根 Aodi ai=new Aodi(); ai.setJiage(100000.0); ai.setXinghao("a6L"); ai.drive(); Benchi bc=new Benchi(); bc.changeSpeed(20); } }
运行结果: