创建一个名称为Vehicle的接口,在接口中添加两个带有一个参数的方法start()和stop()。在两个名称分别为Bike和Bus的类中实现Vehicle接口。创建一个名称为interfaceDemo的类,在interfaceDemo的main()方法中创建Bike和Bus对象,并访问start()和stop()方法。
Bike .java
package MXDX22; public class Bike implements Vehicle { public void start(int a){ System.out.println("自行车发动,速度:"+a); } public void stop(int b){ System.out.println("自行车停止"); } }
Bus.java
package MXDX22; public class Bus implements Vehicle { public void start(int a){ System.out.println("公交车发动,速度:"+a); } public void stop(int b){ System.out.println("公交车停止"); } }
interfaceDemo.java
package MXDX22; public class interfaceDemo { public static void main(String[] args) { // TODO Auto-generated method stub Bike bk=new Bike(); bk.start(15); bk.stop(0); Bus bs=new Bus(); bs.start(56); bs.stop(0); } }
Vehicle.java
package MXDX22; public interface Vehicle { void start(int a); void stop(int b); }