具有继承接口的Java重载方法 [英] Java Overload method with inherited interface
本文介绍了具有继承接口的Java重载方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试理解java的行为。使用此接口:
i'm trying to understand java behaviour. Using this interfaces :
public interface IA {}
public interface IB extends IA {}
public class myClass implements IB {}
我正在重载这样的方法:
I'm overloading a method like this :
public void method(IA a);
public void method(IB b);
使用以下对象调用方法时:
When calling method with the following object :
IA a = new myClass();
method(a);
为什么java使用:
public void method(IA a);
而不是
public void method(IB b);
?
谢谢
推荐答案
因为编译器只知道 a
是 IA
。根据所涉及的表达式的编译时类型,在编译时确定重载, a
的编译时类型是 IA
。
Because the compiler only knows that a
is an instance of IA
. Overloads are determined at compile time based on the compile-time types of the expressions involved, and the compile-time type of a
is IA
.
(将此与覆盖进行比较,根据所涉及的实际类型,在执行时选择方法实现。)
(Compare this with overriding, where the method implementation is chosen at execution time based on the actual type involved.)
这篇关于具有继承接口的Java重载方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文