多态性和接口
如果我有两个类 x 和 y,它们都扩展类 w。 x 实现接口 z。如果我有方法 doSomething(w object) 和 doSomething(x object),如果我调用 doSomething(x) 会发生什么?
编辑: 我在java上实现这个,更具体地说是在android上。 我问这个是因为一些实现特定接口的类在调用 doSomething() 时大多会做同样的事情。但我想特别指出一些特殊情况。
if i have two classes x and y, both extend class w. and x implementing interface z. if i have methods doSomething(w object) and doSomething(x object), what would happen if i call doSomething(x)?
edit:
im implementing this on java, more specifically on android.
im asking this because some classes which implement a specific interface mostly does the same thing when doSomething() is called. but there are special cases which i would like to single out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这取决于您使用的语言。
例如,在 C# 中,它将使用 doSomething(x object) 而不是 doSomething(w object)。
但是,如果您将其转换为 w 那么它将使用 doSomething(w object) ,如下所示:
或
It depends on the language that you're using.
For instance, in C# it would use doSomething(x object) not doSomething(w object).
However, if you casted it to w then it would use doSomething(w object) like this:
or
假设您
传递 object1 将执行
doSomething(w object)
并传递 object2doSomething(x object)
。PS:当然取决于语言(谈到了C#)
PPS:添加了参数名称以使其更清晰
Let's say you have
Passing object1 will execute
doSomething(w object)
and passing object2doSomething(x object)
.P.S: Of course depending on the language (talked about C#)
P.P.S: Added parameter names to make it clearer
在 C# 中,编译器将根据变量声明的类型而不是存储在其中的实际类型来选择正确的方法。
请注意,下面的代码将
W
声明为一个类,并构造它的一个实例。如果将W
设为接口,并删除其声明和构造,则x
和y
的行为将与以下程序相同在这种情况下,W
的接口或类并不重要。让我向您展示其中的区别:
这里我声明了三个类型为
W
、X
和Y
的变量,并调用doSomething< /code> 逐一传递三个变量。该程序的输出是:
正如预期的那样,编译器将选择具有最适合参数类型的方法,并且对于
x
变量,它有一个可以采用以下对象的方法:输入X
。但是,由于类继承,我们可以更改变量的声明,但保留构造的对象类型,因此更改代码如下:
现在输出:
因此
x
变量包含一个事实X
类型的对象没有考虑在内,编译器从变量类型中选择方法,而不是其内容。在 C# 4.0 中,您现在拥有
dynamic
类型,因此再次将代码更改为:再次输出:
因为现在编译器将选择任何方法推迟到运行时,并且在运行时,它会看到变量名为
x
实际上包含一个X
类型的对象,然后选择具有最适合参数类型的方法。In C#, the compiler will pick the right method depending on the declared type of the variable, not on the actual type stored in it.
Note, the code below declares
W
to be a class, and constructs an instance of it. If you makeW
an interface, and remove its declaration and construction, you'll get the same behavior forx
andy
as the program below, interface or class forW
in this case does not matter.Let me show you the difference:
Here I declare three variables, of type
W
,X
, andY
, and calldoSomething
passing the three variables, one by one. The output of this program is:As expected, the compiler will pick the method that has the best fitting parameter type, and in the case of the
x
variable, it has a method that can take an object of typeX
.However, due to class inheritance, we can change the declaration of the variables, but keep the object types constructed, so changing the code like this:
This now outputs:
So the fact that the
x
variable contained an object of typeX
didn't factor into it, the compiler picked the method from the variable type, not its contents.In C# 4.0, you now have the
dynamic
type, so again changing the code to:again outputs:
as now the compiler defers picking any method at all until runtime, and at runtime, it sees that the variable named
x
actually contains an object of typeX
and then picks the method with the best fitting parameter type.你不能有两个不同的方法具有相同的签名。它的代码不明确,编译器不会编译,解释器会抛出错误。
you cant have two different methods with the same signatures. Its ambiguous code, compilers wont compile and interpreters will throw an error.