如何在C#中实现多个接口(如Collection接口)? [英] How to Implementing More Than One Interface in C# like Collection interface ?

查看:180
本文介绍了如何在C#中实现多个接口(如Collection接口)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想为一个类实现两个接口,并且仅对Microsoft Collection< &界面添加功能.有可能

我将链接链接到项目链接

和附加的图片链接是图片链接

致以问候,
Jophy

Hello All,

I want to implement two interfaces to one class and use only one definition for function like Microsoft Collection< T > interface Add function . Is it possible

I attach a link to the project Link

and image attached link is Image link

With regards ,
Jophy

推荐答案

此代码可能会对您有所帮助....

This code might help you....

// Declare the English units interface:
interface IEnglishDimensions
{
    float Length();
    float Width();
}

// Declare the metric units interface:
interface IMetricDimensions
{
    float Length();
    float Width();
}

// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
    float lengthInches;
    float widthInches;

    public Box(float length, float width)
    {
        lengthInches = length;
        widthInches = width;
    }

    // Explicitly implement the members of IEnglishDimensions:
    float IEnglishDimensions.Length()
    {
        return lengthInches;
    }

    float IEnglishDimensions.Width()
    {
        return widthInches;
    }

    // Explicitly implement the members of IMetricDimensions:
    float IMetricDimensions.Length()
    {
        return lengthInches * 2.54f;
    }

    float IMetricDimensions.Width()
    {
        return widthInches * 2.54f;
    }

    static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);

        // Declare an instance of the English units interface:
        IEnglishDimensions eDimensions = (IEnglishDimensions)box1;

        // Declare an instance of the metric units interface:
        IMetricDimensions mDimensions = (IMetricDimensions)box1;

        // Print dimensions in English units:
        System.Console.WriteLine("Length(in): {0}", eDimensions.Length());
        System.Console.WriteLine("Width (in): {0}", eDimensions.Width());

        // Print dimensions in metric units:
        System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
        System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
    }
}
/* Output:
    Length(in): 30
    Width (in): 20
    Length(cm): 76.2
    Width (cm): 50.8
*/


1.我不确定我是否理解您为什么要这样做.
2.如果您确实需要执行此类操作,那么也许您需要重新考虑设计.
3.从逻辑观点来看,成员8070578中的代码似乎是正确的,但是您必须查看它是否对您有帮助. (尽管如此,该答案还是+5,以提供一些有效的代码.)
4.我有一个小例子,您可以在一个类中实现具有相同名称的所有接口函数,然后在外部公开一个函数.在此单个函数中,您将必须查看需要调用的实际实现. (尽管这是有效的代码,但我完全不建议这样做.就好像我们在做这样的事情一样,这是一个严重的设计缺陷)

1. I am not sure I understood why you might want to do this.
2. If you really NEED to do something like this then perhaps you need to rethink design.
3. code from Member 8070578 seems correct from logic standpoint but you will have to see if it helps you. (nevertheless +5 to that answer for giving some working code.)
4. I have a small example in which you can implement all interface functions with same name in a class and then expose a single function outside. inside this single function you will have to see which actual implementation needs to be called. (although this is working code, i would not recommend this at all. as if we are doing something like this their is some serious design flaw)

namespace C1L1
{
    interface one
    {
        int Add(int a, int b);
    }

    interface Two
    {
        int Add(int a, int b);
    }

    public enum TYPE
        {
            SOME_TYPE,
            SOME_OTHER_TYPE
        }

    class dummy : one, Two
    {
        TYPE type_;
        public dummy(TYPE type)
        {
            type_ = type;
        }
        #region Two Members

        int Two.Add(int a, int b)
        {
            Console.WriteLine("Two Add called");
            return a + b;
        }

        #endregion

        #region one Members

        int one.Add(int a, int b)
        {
            Console.WriteLine("One Add called");
            return a + b;
        }

        #endregion

        public int Add(int a, int b)
        {
            Console.WriteLine("common Add called");
            int result;
            if (type_ == TYPE.SOME_TYPE)
            {
                result = ((one)this).Add(a, b);
            }
            else
            {
                result = ((Two)this).Add(a, b);
            }
            
            return result;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            dummy d1 = new dummy(TYPE.SOME_TYPE);
            Console.WriteLine(d1.Add(2, 3).ToString());

            dummy d2 = new dummy(TYPE.SOME_OTHER_TYPE);
            Console.WriteLine(d2.Add(2, 3).ToString());
           
        }       
    }
}


大家好,
我明白了.... ICollection< T>的代码参见is
Hello All,
I get it .... the code of ICollection<T> See is
public class Collection<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
    {


真正的代码具有两个Add函数的实现


really the code have the implementation of two Add functions

public void Add(T item){}

And 
void IList.Add(object item) { this.Add((T) item); } // explicit implementation



如果使用DeCompiler进行查看,则可以在List< t>下看到该实现.像这样,



If you see with DeCompiler, you can see this implementation under List<t> like this,

private int System.Collections.IList.Add(object item)
   {
       List<T>.VerifyValueType(item);
       this.Add((T)item);
       return this.Count - 1;
   }


因为此作用域是IList.Add的私有范围,所以您无法在对象浏览器中看到.
谢谢大家


since this scope is private for IList.Add So you are not able to see in object browser.
Thank you All


这篇关于如何在C#中实现多个接口(如Collection接口)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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