接口实现方式(c#)

如果类实现两个接口,并且这两个接口包含具有相同签名的成员,那么在类中实现该成员将导致两个接口都使用该成员作为它们的实现。例如:

//接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{

    interface IControl
    {
        void Paint();
    }
    interface ISurface
    {
        void Paint();
    }

}


//接口实现类(隐式实现)
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Class2:IControl,ISurface
    {

        #region ISurface 成员

        public void Paint()
        {
            Console.WriteLine("Hello,World!");
        }

        #endregion
    }
}

//效果测试类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class2 c2 = new Class2();
            c2.Paint();

            Console.ReadLine();
        }
    }
}

然而,如果两个接口成员执行不同的函数,那么这可能会导致其中一个接口的实现不正确或两个接口的实现都不正确。可以显式地实现接口成员 -- 即创建一个仅通过该接口调用并且特定于该接口的类成员。这是使用接口名称和一个句点命名该类成员来实现的。例如:

//接口
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    interface ITest
    {
        bool GetResult(string userName);

        int P { get;}
    }

    interface ITest1
    {
        int P(int a,int b);

        bool GetResult(int age);
    }

}

//接口实现类(显示实现)
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Class1:ITest,ITest1
    {
        private int _a;

        public Class1(int a)
        {
            this._a = a;
        }

        #region ITest 成员

        bool ITest.GetResult(string userName)
        {
            bool check = false;
            if (userName == "xiaoming")
            {
                check = true;
            }
            return check;
        }

        public int P
        {
            get { return _a; }
        }

        #endregion

        #region ITest1 成员

        int ITest1.P(int a,int b)
        {
            return a + b;
        }

        bool ITest1.GetResult(int age)
        {
            bool check = false;
            if (age >= 18)
            {
                check = true;
            }
            return check;
        }

        #endregion
    }
}


//测试效果类
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Class1 c = new Class1(100);
            int num = c.P;
            ITest t1 = (ITest)c;
            bool check = t1.GetResult("zhangsan");

            ITest1 t = (ITest1)c;
            int sum = t.P(4, 5);
            bool check1 = t.GetResult(22);

            Console.WriteLine("ITest:");
            Console.WriteLine("FunctioName:GetResult-->" + check.ToString());
            Console.WriteLine("AttributeName:P-->" + num.ToString());
            Console.WriteLine("");
            Console.WriteLine("ITest1:");
            Console.WriteLine("FunctionName:P-->" + sum.ToString());
            Console.WriteLine("FunctionName:GetResult-->" + check1.ToString());

            Console.ReadLine();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值