C#的interface关键字
来源:互联网 发布:扫描字的软件 编辑:程序博客网 时间:2024/05/30 23:01
An interface contains only the signatures of methods, delegates or events. The implementation of the methods is done in the class that implements the interface, as shown in the following example:
interface ISampleInterface{ void SampleMethod();}class ImplementationClass : ISampleInterface{ // Explicit interface member implementation: void ISampleInterface.SampleMethod() { // Method implementation. } static void Main() { // Declare an interface instance. ISampleInterface obj = new ImplementationClass(); // Call the member. obj.SampleMethod(); }}
An interface can be a member of a namespace or a class and can contain signatures of the following members:
Methods
Properties
Indexers
Events
An interface can inherit from one or more base interfaces.
When a base type list contains a base class and interfaces, the base class must come first in the list.
A class that implements an interface can explicitly implement members of that interface. An explicitly implemented member cannot be accessed through a class instance, but only through an instance of the interface, for example:
For more details and code examples on explicit interface implementation, see Explicit Interface Implementation (C# Programming Guide).
The following example demonstrates interface implementation. In this example, the interface IPoint
contains the property declaration, which is responsible for setting and getting the values of the fields. The class Point
contains the property implementation.
// keyword_interface_2.cs// Interface implementationusing System;interface IPoint{ // Property signatures: int x { get; set; } int y { get; set; }}class Point : IPoint{ // Fields: private int _x; private int _y; // Constructor: public Point(int x, int y) { _x = x; _y = y; } // Property implementation: public int x { get { return _x; } set { _x = value; } } public int y { get { return _y; } set { _y = value; } }}class MainClass{ static void PrintPoint(IPoint p) { Console.WriteLine("x={0}, y={1}", p.x, p.y); } static void Main() { Point p = new Point(2, 3); Console.Write("My Point: "); PrintPoint(p); }}
Output
My Point: x=2, y=3
- C#的interface关键字
- C#关键字—virtual 、 abstract、interface 的区别
- C#关键字: Sealed, Abstract, Virtual和Interface
- C#关键字abstract virtual interface new override
- C#的主要 关键字 new、sealed、partial、interface、virtual、abstract、this、base、override、return、break、continue...
- 关键字interface
- 关键字interface
- Final关键字,interface关键字
- C#类型推断(type interface)使用var关键字。
- C# 中InterFace的认识
- c#中Interface的理解
- C# 接口interface的学习
- C# Interface
- C#的关键字 [STAThread]
- C#的关键字 [STAThread]
- C#的is关键字
- C#的override关键字
- C#的Virtual关键字
- isight5 二次开发之 Application (2) 之 Calc组件
- 面试体会
- <cf> Prizes, Prizes, more Prizes
- C语言插件机制(下)
- 对于排序 学习心得
- C#的interface关键字
- 使用WCF实现服务器分页
- JAVA 环境变量设置
- poj1149(最大流,构图较难)
- Explicit Interface Implementation
- netbean 乱码 (运行 编码 UTF-8 的不可映射字符)
- hdu 1875畅通工程再续-prim最小生成树
- 配置struts2和spring3
- JSF 2.0 中定义受管 Bean 的三种途径的比较