24.编写一个Car类,具有String类型的属性品牌,具有功能drive;
定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速;
定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特
性。
package zuoye; public class Car { public String pinpai; public String getPinpai() { return pinpai; } public void setPinpai(String pinpai) { this.pinpai = pinpai; } public void drive() { System.out.println("具有功能变速"); } public String getInfo() { return pinpai; } }
package zuoye; public class Benchi extends Car { public double jiage; public 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 void biansu() { System.out.println("bian su"); } public String getInfo() { return xinghao+jiage+getPinpai(); } }
package zuoye; public class E { public static void main(String[] args) { Aodi a =new Aodi(); Benchi b =new Benchi(); a.setPinpai("奥迪"); a.setXinghao("R8"); a.setJiage(2000000); b.setPinpai("奔驰"); b.setXinghao("很贵"); b.setJiage(12345567); System.out.println(a.getInfo()); System.out.println(b.getInfo()); } }
时间: 2024-10-26 22:56:15