案例(根据上面的猫狗案例,完成下面的汽车案例)
利用面向对象的思想,结合猫狗的案例,仔细分析下面的描述,实现需求!
描述:汽车都具有跑的功能,普通的奥迪车也不例外,但是高端的奥迪车除了具有跑的功能外,还具有自动泊车和无人驾驶的功能!
需求:定义普通奥迪车,高端奥迪车,实现描述中的功能并测试
//-------------------------------------------------------------------------
class Demo4_Car {
public static void main(String[] args) {
Car c1= new NormalAudi("普通奥迪");
c1.run();
Car c2= new HighEndAudi("高端奥迪");
c2.run();
HighEndAudi hea =(HighEndAudi)c2;
hea.autoParking();
hea.unmanned();
}
}
abstract class Car {
private String name;
public Car(){}
public Car(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public abstract void run();
}
interface AutoParking {
void autoParking();
}
interface Unmanned {
void unmanned();
}
class NormalAudi extends Car{
private String name;
public NormalAudi(){}
public NormalAudi(String name){
super(name);
}
public void run(){
System.out.println("普通奥迪跑起来了");
}
}
class HighEndAudi extends Car implements AutoParking,Unmanned {
private String name;
public HighEndAudi(){}
public HighEndAudi(String name){
super(name);
}
public void run(){
System.out.println("高端奥迪跑起来了");
}
public void autoParking(){
System.out.println("自动泊车");
}
public void unmanned(){
System.out.println("无人驾驶");
}
}
|
|