实现两个定义相同方法的接口的类

发布于 2024-10-10 22:42:56 字数 400 浏览 10 评论 0原文

//inteface i11 
 public interface i11
    {
        void m11();
    }
//interface i22
    public interface i22
    {
         void m11();
    }

//class ab implements interfaces i11 and i22
    public class ab : i11, i22
    {
        public void m11()
        {
            Console.WriteLine("class");
        }
    }

现在,当我们在 Main() 方法中创建一个 ab 类的对象时 将调用哪个接口方法,请全部解释。

//inteface i11 
 public interface i11
    {
        void m11();
    }
//interface i22
    public interface i22
    {
         void m11();
    }

//class ab implements interfaces i11 and i22
    public class ab : i11, i22
    {
        public void m11()
        {
            Console.WriteLine("class");
        }
    }

now when in Main() method we create an object of class ab
which interface method will get called please explain all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

陈独秀 2024-10-17 22:42:56

只有一种方法实现,因此显然无论引用的类型如何,都会调用该方法。 IE:

i11 o = new ab();
o.m11();

i22 o = new ab();
o.m11();

实际上是相同的。

ab.m11 满足两个接口中方法的签名。另外,您没有使用显式接口实现< /a>,所以这不是一个因素。

There's only one method implementation, so obviously that gets called, regardless of the type of the reference. I.E.:

i11 o = new ab();
o.m11();

and

i22 o = new ab();
o.m11();

are effectively the same.

ab.m11 satisfies the signature of the method in both interfaces. Also, you're not using explicit interface implementation, so that isn't a factor.

浅忆 2024-10-17 22:42:56

接口只是说“任何实现此接口的东西都可以确保具有这些方法”。
如果您在这种情况下考虑它,您会发现它不会调用接口的方法;而是调用接口的方法。相反,它只是实现所需的方法调用。

Interfaces merely say "Anything that implements this interface can be sure to have these methods".
If you think about it in that context, you'll see that it doesn't call the interface's method; rather, it merely implements the method calls required.

路弥 2024-10-17 22:42:56

您将接口的实现与具体类的扩展混淆了。我相信这会让你对这段关系产生倒退的想法。根据定义,接口没有具体的实现。它仅定义实现类必须使用的契约。也许一个例子可以帮助澄清。

假设我们有一个名为 IBank 的接口。该接口具有以下定义:

IBank
{
    void AddMoney(int amount);
    void RemoveMoney(int amount);
    int GetBalance();
}

然后我们对该接口的具体实现如下:

EuroBank : IBank
{
    void AddMoney(int amount){ balance+= amount; }
    void RemoveMoney(int amount){ balance-= amount; }
    int GetBalance(){ return balance; }
    private int balance;
}

我无法创建 IBank 的新实例。以下代码将无法编译。

IBank bank = new IBank;

相反,我必须创建一个新的具体银行实例,我可以将其视为 IBank。然后我可以调用这个类的方法。

IBank bank = new EuroBank;
bank.AddMoney(7);

因此,当我调用 AddMoney 时,我使用了接口定义的方法,但实际上是在 EuroBank 类上调用 AddMoney 的具体实现。

因此,在您的示例中,您正在调用 ab 类上的 m11 方法。但是,ab 同时实现了 i11i22。因此,它可以被视为 i11i22 对象。以下两行代码都将起作用。

i11 first = new ab();
i22 second = new ab();

我可以在任一对象上调用 m11 方法,但它将始终使用在对象 ab 上找到的方法的具体实现。

You are confusing the implementation of an interface with the extension of concrete class. I believe this is causing you to think of the relationship backwards. An interface, by definition, has no concrete implementation. It only defines a contract that implementing classes must use. Perhaps an example can help clarify.

Suppose we have an interface called IBank. This interface has the following definition:

IBank
{
    void AddMoney(int amount);
    void RemoveMoney(int amount);
    int GetBalance();
}

Then we have a concrete implementation of this interface as follows:

EuroBank : IBank
{
    void AddMoney(int amount){ balance+= amount; }
    void RemoveMoney(int amount){ balance-= amount; }
    int GetBalance(){ return balance; }
    private int balance;
}

I cannot create a new instance of IBank. The following code will not compile.

IBank bank = new IBank;

Instead, I must create a new concrete instance of a bank that I can treat as an IBank. I can then call methods on this class.

IBank bank = new EuroBank;
bank.AddMoney(7);

So when I call AddMoney, I using the method defined by the interface, but I am actually calling the concrete implementation of AddMoney on the EuroBank class.

So in your example, you are calling the m11 method on the ab class. However, ab implements both i11 and i22. Therefore, it can be treated as both an i11 and i22 object. Both of the following lines of code will work.

i11 first = new ab();
i22 second = new ab();

I can call the m11 method on either object, but it will always use the concrete implementation of the method found on the object ab.

少女情怀诗 2024-10-17 22:42:56

该方法不是接口方法。
将调用的方法是类abm11 方法。

The method is not an interface method.
The method that will be called is the m11 method of class ab.

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