显式接口成员实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Hello_leiyuanyi/article/details/81262102

  显示接口成员实现中不能包含访问符、abstract、virtual、override、或static修饰符。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 显式接口成员实现
{
    interface ICalculate1
    {
        int Add();
    }
    interface ICalculate2
    {
        int Add();   //求和方法,加法运算的和
    }

    class Compute : ICalculate1, ICalculate2
    {
        int ICalculate1.Add()  //显示接口成员实现
        {
            int x = 10;
            int y = 40;
            return x + y;
        }
        //显示接口成员实现
        int ICalculate2.Add()
        {
            int x = 10;
            int y = 40;
            int z = 50;
            return x + y + z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Compute compute = new Compute();  //实例化接口继承类的对象
            ICalculate1 Call = compute;  //使用接口继承类的对象实例化接口
            Console.WriteLine(Call.Add());  //使用接口对象调用接口中的方法

            ICalculate2 Cal2 = compute;
            Console.WriteLine(Cal2.Add());

            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Hello_leiyuanyi/article/details/81262102