读书人

Java接口的Bug

发布时间: 2012-12-19 14:13:14 作者: rapoo

Java接口的Bug?

请大家先来看几个接口和类:

?首先请看接口,注意它们所在的包,他们拥有共同的方法:run()

package cn.com.oo.util;public interface First {void run();}

?

package cn.com.oo.util;public interface Second {void run();}
package cn.com.oop.util;public interface Three {void run();}

??

?

其次看实现类:

package cn.com.oo.util;public class ImplFirst implements First{public void run() {System.out.println("ImplFirst");}}

?

package cn.com.oo.util;public class ImplSecond implements Second{public void run() {System.out.println("ImplSecond");}}

?

package cn.com.oo.util;import cn.com.oop.util.Three;public class ImplThree implements Three{public void run() {System.out.println("ImplThree");}}

?

package cn.com.oo.util;import cn.com.oop.util.Three;public class ImplInterfaces implements Three,Second,First {public static void main(String[] args) {First first = new ImplFirst();Second second = new ImplSecond();Three three = new ImplThree();first.run();second.run();three.run();new ImplInterfaces().run();}public void run() {System.out.println("ImplInterfaces");}}

?

下面我们把实现方法注释:

Java接口的Bug

?

默认实现的是第一个接口的方法,而后面接口的方法被和谐!

?

Java接口的Bug

大家看到new ImplInterfaces().run();运行结果是本类的方法,根本不是实现过来的;

而如果我们把上面的first.run();??? second.run();????three.run();??? 注释会得到同样的结果!

?

不知为何,请高手们赐教,不甚感激!

?

?

?

?

?

?

?

?

?

读书人网 >编程

热点推荐