如果2个接口具有相同的方法,如何实现c#代码 [英] How to implement a c# code if 2 interfaces has same methods

查看:116
本文介绍了如果2个接口具有相同的方法,如何实现c#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

interface iSame
    {
        void Method();
    }
    interface iSame1
    {
        int Method();
    }

    public partial class Form1 : Form, iSame, iSame1
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Method()
        {
            MessageBox.Show("This is iSame");
        }

        public int Method()
        {
            MessageBox.Show("This is iSame");
            return 1;
       }





如何使用不同接口的相同方法名称实现此应用程序?





How can i implement a this application with same method names of different interfaces ?

This gives an error saying already Method is implemented. But if you remove the int Method then also it gives compilation error saying Error    2   'WindowsFormsApplication1.Form1' does not implement interface member 'WindowsFormsApplication1.iSame1.Method()'. 'WindowsFormsApplication1.Form1.Method()' cannot implement 'WindowsFormsApplication1.iSame1.Method()' because it does not have the matching return type of 'int'.  F:\C#\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    22  26  WindowsFormsApplication1

If both Tests have the same signature, same return type but differ in the types of exceptions they throw, then the class implements only the one Test, but it must contain the exceptions both Test's

If arguments are different we can implement without any error.

推荐答案

首先,方法不一样;它们只有相同的名称和签名。方法标识由程序集/类型/方法的组合构成,而不仅仅是名称。如果程序集已签名,它甚至会使每个方法都是世界唯一的,即使其他方法具有相同的名称/签名。



您只需要编写显式接口实现,而不是隐式。一般来说,显式提供了许多隐含的好处。



以下是如何:

First of all, the methods are not the same; they only have the same name and signature. Method identity is made of the combination of assembly/type/method, not just name. If an assembly is signed, it even makes each method world-unique, even if other methods have the same name/signature.

You simply need to write explicit interface implementations instead of implicit. Generally, the explicit provides many benefits over the implicit.

Here is how:
interface IFirst { void Method(); }
interface ISecond { void Method(); }

class SomeClass: Form, IFirst, ISecond { // here is where the problem starts
    //this is how it is solved:
    void IFirst.Method() { /* ... */ }
    void ISecond.Method() { /* ... */ }
} //class SomeClass

// now, the instances:

SomeClass classInstance = new SomeClass();
// you cannot use IFirst or ISecond members without a type cast with this variable,
// which is good, because it provides proper isolation

IFirst classInstanceAsIFirst = classInstance; // can use IFirst members
ISecond classInstanceAsISecond = classInstance; // same runtime object, but now you can use ISecond members





我希望这一切都清楚。



另请参阅这篇旧的但仍然完全有效的MSDN文章: http://msdn.microsoft.com/en-us/library/aa288461% 28v = vs.71%29.aspx [ ^ ]。



MSDN手册中的最新章节:

http://msdn.microsoft.com/en-us/library/ms173157.aspx [ ^ ],

< a href =http://msdn.microsoft.com/en-us/library/tx1s9z1w.aspx> http://msdn.microsoft.com/en-us/library/tx1s9z1w.aspx [< a href =http://msdn.microsoft.com/en- us / library / tx1s9z1w.aspxtarget =_ blanktitle =New Window> ^ ]。



-SA



I hope this is all clear.

See also this old but still fully valid MSDN article: http://msdn.microsoft.com/en-us/library/aa288461%28v=vs.71%29.aspx[^].

Most recent chapters in MSDN manual:
http://msdn.microsoft.com/en-us/library/ms173157.aspx[^],
http://msdn.microsoft.com/en-us/library/tx1s9z1w.aspx[^].

—SA


此代码项目文章 C#中的接口(适用于初学者) [ ^ ]有一个关于这个问题的部分(在文章中查找P13.cs)
This code project article Interfaces in C# (For Beginners)[^] has a section on this very problem (look for "P13.cs" in the article)


这篇关于如果2个接口具有相同的方法,如何实现c#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆