日期:2014-05-20 浏览次数:20907 次
public class InterfaceDemo {
public static void main(String[] args) {
MInterface[] ary=new MInterface[5];
ary[0]=new A();
ary[1]=new B();
ary[2]=new C();
ary[3]=new B();
ary[4]=new A();//接口的具体实现类
for(MInterface m:ary){
m.move();//java的自动向下绑定会自动实现具体实现类的move()方法
}
}
}
class A implements MInterface{
@Override
public void move() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("这一行代表A对接口方法的业务逻辑实现");
}
}
class B implements MInterface{
@Override
public void move() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("这一行代表B对接口方法的业务逻辑实现");
}
}
class C implements MInterface{
@Override
public void move() {
//To change body of implemented methods use File | Settings | File Templates.
System.out.println("这一行代表C对接口方法的业务逻辑实现");
}
}
interface MInterface{
public void move();
}
------解决方案--------------------
1楼的
MInterface m = new A();
你用m.move();只能调用A的实现方法
用B的时候 你还不是要重新new B()出来。
你这个还是木有说出其中的好处。