[英]questions for dynamic binding and signature method in java ab
Matching a method signature and binding a method implementation are two sepearate issue so ,what's wrong with the follows code? 匹配方法签名和绑定方法实现是两个不同的问题,因此,以下代码有什么问题?
Container c5 = new JButton();
Object c6 = new JButton();
c5.add(c6); //----it is wrong,why?
for me , c5 is a reference variable which contain reference to object of JButton,and JButton extends the class Component,so it should be right so why ? 对我来说,c5是一个引用变量,其中包含对JButton对象的引用,并且JButton扩展了Component类,所以应该正确,为什么?
Java is a Single-Dispatch language. Java是单调度语言。 That means its method signature analysis is done at compile time, not run time. 这意味着其方法签名分析是在编译时而不是运行时完成的。
The type of c6
is Object
. c6
的类型为Object
。 (Yes, you assign a JButton
, but the declared type of the variable is still Object
.) So when Java looks at c5
(a Container
) and its methods, it doesn't see an add()
method that takes an Object. (是的,您分配了一个JButton
,但是变量的声明类型仍然是Object
。)因此,当Java查看c5
(一个Container
)及其方法时,它看不到采用Object的add()
方法。 So it flags it as an error. 因此,将其标记为错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.