24.编写一个Car类,具有String类型的属性品牌,具有功能drive; 定义其子类Aodi和Benchi,具有属性:价格、型号;具有功能:变速; 定义主类E,在其main方法中分别创建Aodi和Benchi的对象并测试对象的特 性。
//Car
package zuoye;
public class Car {
private String pinpai;
public String getPinpai() {
return pinpai;
}
public void setPinpai(String pinpai) {
this.pinpai = pinpai;
}
public int drive()
{
int i=100;
return i;
}
}
//Aodi
package zuoye;
public class Aodi extends Car {
private double price;
private String style="";
public Aodi() {
super();setPinpai("Aodi");
}
public void changeSpeed(int j)
{
System.out.println(getPinpai()+getStyle()+"速度变为:"+(this.drive()+j));
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
}
//Benchi
package zuoye;
public class Benchi extends Car {
private double price;
private String style="";
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public void changeSpeed(int j)
{
System.out.println(getPinpai()+getStyle()+"速度变为:"+(drive()+j));
}
public Benchi() {
super();
setPinpai("Benchi");
}
}
//主类
package zuoye;
public class E {
public static void main(String[] args) {
Aodi a=new Aodi();
Benchi b=new Benchi();
a.setStyle("Q7");
b.setStyle("S200");
a.drive();
b.drive();
a.changeSpeed(20);
a.changeSpeed(-20);
b.changeSpeed(-50);
}
}
///运行结果
AodiQ7速度变为:120
AodiQ7速度变为:80
BenchiS200速度变为:50