文章目录
接口
什么是接口
使用关键字interface创建的数据类型
接口的特点
接口:支持多继承
接口中不能定义字段
接口中定义的方法不能包含方法体
接口定义了属性、方法和事件,这些都是接口的成员
接口中的成员不允许使用 static、virtual、abstract、sealed 修饰符
接口中的成员不允许使用 public、private、protected、internal 访问修饰符
接口的应用场景
根据需求进行定义接口,多次继承使用时或需采用接口的形式进行实现
接口实现案例
本案例为C# 控制台应用程序案例,主要实现接口的创建、调用及赋值,大家简单了解一下吧,上干货!!!
定义接口
public interface InterfaceMethod_1 { /** * 定义接口 * **/ int Delete(int a, int b); }
定义接口实现类
public interface InterfaceMethod { /** * 定义接口方法 * **/ int Add(int a,int b); } /* * 接口实现类 * 继承接口类 * **/ public class interfaceClass : InterfaceMethod, InterfaceMethod_1 { public int Add(int a, int b) { return a + b; } public int Delete(int a, int b) { return a - b; } }