多态性和接口

发布于 2024-09-02 01:33:08 字数 222 浏览 2 评论 0原文

如果我有两个类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

櫻之舞 2024-09-09 01:33:08

这取决于您使用的语言。

例如,在 C# 中,它将使用 doSomething(x object) 而不是 doSomething(w object)。

但是,如果您将其转换为 w 那么它将使用 doSomething(w object) ,如下所示:

doSomething((w) someObjectOfX);

doSomething(someObjectOfX as w);

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:

doSomething((w) someObjectOfX);

or

doSomething(someObjectOfX as w);
场罚期间 2024-09-09 01:33:08

假设您

w object1 = new x();
x object2 = new x();

传递 object1 将执行 doSomething(w object) 并传递 object2 doSomething(x object)

PS:当然取决于语言(谈到了C#)

PPS:添加了参数名称以使其更清晰

Let's say you have

w object1 = new x();
x object2 = new x();

Passing object1 will execute doSomething(w object) and passing object2 doSomething(x object).

P.S: Of course depending on the language (talked about C#)

P.P.S: Added parameter names to make it clearer

伤感在游骋 2024-09-09 01:33:08

在 C# 中,编译器将根据变量声明的类型而不是存储在其中的实际类型来选择正确的方法。

请注意,下面的代码将 W 声明为一个类,并构造它的一个实例。如果将 W 设为接口,并删除其声明和构造,则 xy 的行为将与以下程序相同在这种情况下,W 的接口或类并不重要。

让我向您展示其中的区别:

using System;

namespace SO2851194
{
    class W { }
    class X : W { }
    class Y : W { }

    class Program
    {
        static void Main()
        {
            W w = new W();
            X x = new X();
            Y y = new Y();

            doSomething(w);
            doSomething(x);
            doSomething(y);
        }

        static void doSomething(W w)
        {
            Console.Out.WriteLine("w");
        }

        static void doSomething(X x)
        {
            Console.Out.WriteLine("x");
        }
    }
}

这里我声明了三个类型为 WXY 的变量,并调用 doSomething< /code> 逐一传递三个变量。该程序的输出是:

w
x
w

正如预期的那样,编译器将选择具有最适合参数类型的方法,并且对于 x 变量,它有一个可以采用以下对象的方法:输入X

但是,由于类继承,我们可以更改变量的声明,但保留构造的对象类型,因此更改代码如下:

W w = new W();
W x = new X();   // Notice, changed variable type to W
W y = new Y();   // but keep constructing X and Y

现在输出:

w
w
w

因此 x 变量包含一个事实X 类型的对象没有考虑在内,编译器从变量类型中选择方法,而不是其内容。

在 C# 4.0 中,您现在拥有 dynamic 类型,因此再次将代码更改为:

dynamic w = new W();
dynamic x = new X();
dynamic y = new Y();

再次输出:

w
x
w

因为现在编译器将选择任何方法推迟到运行时,并且在运行时,它会看到变量名为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 make W an interface, and remove its declaration and construction, you'll get the same behavior for x and y as the program below, interface or class for W in this case does not matter.

Let me show you the difference:

using System;

namespace SO2851194
{
    class W { }
    class X : W { }
    class Y : W { }

    class Program
    {
        static void Main()
        {
            W w = new W();
            X x = new X();
            Y y = new Y();

            doSomething(w);
            doSomething(x);
            doSomething(y);
        }

        static void doSomething(W w)
        {
            Console.Out.WriteLine("w");
        }

        static void doSomething(X x)
        {
            Console.Out.WriteLine("x");
        }
    }
}

Here I declare three variables, of type W, X, and Y, and call doSomething passing the three variables, one by one. The output of this program is:

w
x
w

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 type X.

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:

W w = new W();
W x = new X();   // Notice, changed variable type to W
W y = new Y();   // but keep constructing X and Y

This now outputs:

w
w
w

So the fact that the x variable contained an object of type X 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:

dynamic w = new W();
dynamic x = new X();
dynamic y = new Y();

again outputs:

w
x
w

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 type X and then picks the method with the best fitting parameter type.

北风几吹夏 2024-09-09 01:33:08

你不能有两个不同的方法具有相同的签名。它的代码不明确,编译器不会编译,解释器会抛出错误。

you cant have two different methods with the same signatures. Its ambiguous code, compilers wont compile and interpreters will throw an error.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文