首先了解接口,抽象类和具体类的需求.
让我们在这里举个例子:
public interface Vehicle{
public void startEngine();
public void run();
}
public abstract class Bus implements Vehicle{
public void startEngine(){
System.out.println("Engine Starting of bus");
}
}
public abstract class Plane implements Vehicle{
public void startEngine(){
System.out.println("Engine Starting of plane");
}
}
public class VolvoBus extends Bus{
public void run(){
System.out.println("Running at 100kmp/h");
}
}
public class NonACBus extends Bus{
public void run(){
System.out.println("Running at 50kmp/h");
}
}
public class Test{
public static void main(String[] args){
VolvoBus volvoBus=new VolvoBus();
NonACBus nonAcbus=new NonACBus();
volvoBus.startEngine();
volvoBus.run();
nonAcBus.startEngine();
nonAcBus.run();
}
}
参见上面的示例,我们有总线通用的代码,无论是交流总线还是沃尔沃,因此都是在Bus类中编写的,但是run()并不是所有人都通用的,因此不是在Bus类中实现而是将其保留为抽象,因此其子类将根据那里的要求实施.
我的代码将更好地向您解释:)
谢谢