编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
package homework1; public class Car { //成员变量 String pinpai; //成员方法 void drive() { } }
package homework1; public class Aodi extends Car { //成员变量 public String jiage; public String xinghao; public double sudu; Aodi(String jiage,String xinghao,double sudu) { this.sudu=sudu; this.jiage=jiage; this.xinghao=xinghao; } //成员方法 double jiasu(double a) { this.sudu+=a; return sudu; } double jiansu(double b) { this.sudu-=b; return sudu; } }
package homework1; public class Benchi extends Car { //成员变量 public String jiage; public String xinghao; public double sudu; Benchi(String jiage,String xinghao,double sudu) { this.sudu=sudu; this.jiage=jiage; this.xinghao=xinghao; } //成员方法 double jiasu(double a) { this.sudu+=a; return sudu; } double jiansu(double b) { this.sudu-=b; return sudu; } }
package homework1; public class E { public static void main(String[] args) { Aodi ad=new Aodi("500000元","A6L",110); System.out.println("品牌:Aodi"+" 型号:"+ad.xinghao+" 价格:"+ad.jiage+" 车速:"+ad.sudu); System.out.println("加速30后车速为:"+ad.jiasu(30)); Benchi bc=new Benchi("900000元","S 320L",120); System.out.println("品牌:Benchi"+" 型号:"+bc.xinghao+" 价格:"+bc.jiage+" 车速:"+bc.sudu); System.out.println("减速30后车速为:"+bc.jiansu(30)); } }