接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。
接口使得实现接口的类或结构在形式上保持一致。
抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。
定义接口:(接口类)
接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。下面是一个接口声明的实例:
ICalc.cs

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

namespace _01接口
{
    interface ICalc:IWork
    {
        int Add(int a, int b);
        int Cheng(int a, int b);
    }
}

IWork.cs

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

namespace _01接口
{
    interface IWork
    {
        void Run();
        void Talk();
        int Add(int a, int b);
    }
}

Program.cs

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

namespace _01接口
{
    class Program: ICalc,IWork
    {
        static void Main(string[] args)
        {

        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        int ICalc.Add(int a, int b)
        {
            return a + b;
        }

        public int Cheng(int a, int b)
        {
            return a * b;
        }

        public void Run()
        {
            
        }

        public void Talk()
        {
            
        }

        int IWork.Add(int a, int b)
        {
            throw new NotImplementedException();
        }
        class People : ICalc
        {
            public int Add(int a, int b)
            {
                return a + b;
            }

            public int Cheng(int a, int b)
            {
                return a * b;
            }

            public void Run()
            {

            }

            public void Talk()
            {
                
            }
        }
    }
}
posted on 2019-02-06 12:55  豆皮没有豆  阅读(101)  评论(0)    收藏  举报