显示实现接口在什么时候用

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSharp内功修炼
{

———————————–显示实现接口代码
public interface Math
{
void Test(T t);
}

public interface Math1
{
    void Test(T t);
}

public class ReallyClass : Math, Math1
{
    void Math.Test(ReallyClass t)
    {
        try
        {
            throw new NotImplementedException();
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message + "Math");
        }
    }

    void Math1.Test(ReallyClass t)
    {
        try
        {
            throw new NotImplementedException();
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message + "Math1");
        }
    }
}
/// 
/// 功能测试入口点
/// 
class Program
{     
    static void Main(string[] args)
    {
        ReallyClass @class = new ReallyClass();
        (@class as Math).Test(@class);
    }


}

}

————————–隐试实现接口
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace CSharp内功修炼
{

public interface Math
{
    void Test(T t);
}

public interface Math1
{
    void Test(T t);
}

public class ReallyClass : Math, Math1
{
    //void Math.Test(ReallyClass t)
    //{
    //    try
    //    {
    //        throw new NotImplementedException();
    //    }
    //    catch (Exception e)
    //    {

    //        Console.WriteLine(e.Message + "Math");
    //    }
    //}

    //void Math1.Test(ReallyClass t)
    //{
    //    try
    //    {
    //        throw new NotImplementedException();
    //    }
    //    catch (Exception e)
    //    {

    //        Console.WriteLine(e.Message + "Math1");
    //    }
    //}
    public void Test(ReallyClass t)
    {
         try
        {
            throw new NotImplementedException();
        }
        catch (Exception e)
        {

            Console.WriteLine(e.Message + "Math1");
        }
    }
}
/// 
/// 功能测试入口点
/// 
class Program
{     
    static void Main(string[] args)
    {
        ReallyClass @class = new ReallyClass();
        (@class as Math1).Test(@class);
        (@class as Math).Test(@class);
        @class.Test(@class);
    }


}

}

我们发现 当我们继承多个接口 但是接口是一样的 也就是函数名 参数 返回类型全部一样 这个时候我们如果想在不同接口下
实现不同的方法体 必须用显示实现接口 并且我们在使用接口的时候 必须先转成对应的接口才可以 这些隐实现接口是做不到了 因为相同接口 就给了一个实现体

你可能感兴趣的:(c#)