c#接口类
来源:互联网 发布:一七网络批发广州 编辑:程序博客网 时间:2024/06/08 12:29
C# 接口(Interface)
接口定义了所有类继承接口时应遵循的语法合同。接口定义了语法合同 "是什么" 部分,派生类定义了语法合同 "怎么做" 部分。
接口定义了属性、方法和事件,这些都是接口的成员。接口只包含了成员的声明。成员的定义是派生类的责任。接口提供了派生类应遵循的标准结构。
接口使得实现接口的类或结构在形式上保持一致。
抽象类在某种程度上与接口类似,但是,它们大多只是用在当只有少数方法由基类声明由派生类实现时。
定义接口:
接口使用 interface 关键字声明,它与类的声明类似。接口声明默认是 public 的。通常接口命令以 I 字母开头。
继承接口后,我们需要实现接口的方法 MethodToImplement() , 方法名必须与接口定义的方法名一致。
code:
using System;interface IMyInterface{ // 接口成员 void MethodToImplement();}class InterfaceImplementer : IMyInterface{ static void Main() { InterfaceImplementer iImp = new InterfaceImplementer(); iImp.MethodToImplement(); } public void MethodToImplement() { Console.WriteLine("MethodToImplement() called."); }}
接口继承: InterfaceInheritance.cs
以下实例定义了两个接口 IChildreninterface 和 IParentInterface。
如果一个接口继承其他接口,那么实现类或结构就需要实现所有接口的成员。
以下实例 Progam 继承了 IParentInterface 和IChildreninterface接口,因此接口实现类必须实现 method()和method2()方法:
code:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace _20170917{ interface Iparentinterface { void method(); } interface Ichildreninterface:Iparentinterface { void method2(); } class Program : Ichildreninterface { static void Main(string[] args) { Program n1 = new Program(); n1.method(); n1.method2(); Console.ReadKey(); } public void method() { Console.WriteLine("parents"); } public void method2() { Console.WriteLine("children"); } }}
阅读全文
0 0
- C# 类 接口 接口
- C# 类和接口
- C# 接口、抽象类
- C# 类与接口
- C#类接口实例
- C# 类与接口
- c#接口类
- 浅谈C#抽象类和C#接口
- .NET C#数据库接口类
- c# 抽象类和接口
- C# 抽象类和接口
- 接口与抽象类 c#
- c#接口和抽象类
- C#抽象类和接口
- C#抽象类和接口
- c#继承(抽象类、接口)
- C#接口和抽象类
- c#抽象类与接口
- Webpack配置方法
- 点灯游戏
- vim configure
- 优雅实现request请求IOC
- C++中的文件尾条件--EOF
- c#接口类
- 使用 bintray 网站发布 library 到 jcenter
- 对于多线程类 实现Runnable接口和继承Thread类有什么区别
- js常用的正则
- Leetcode | Median of Two Sorted Arrays
- loadrunner Web_类函数之web_get_int_property()
- POJ4089电话号码--Trie树的应用
- [部署]VM11下CentOS7mini安装及配置
- 为什么单片机中既有FLASH又有EEPROM?