接口(C# 参考)
来源:互联网 发布:天猫美工培训 编辑:程序博客网 时间:2024/06/02 20:44
以下参考 :http://msdn.microsoft.com/zh-cn/library/87d83y5b.aspx
接口(C# 参考)
更新:2007 年 11 月
接口只包含方法、属性、事件或索引器的签名。成员的实现是在实现接口的类或结构中完成的,如下面的示例所示:
示例
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();
}
}
接口可以是命名空间或类的成员,并且可以包含下列成员的签名:
方法
属性
索引器
事件
一个接口可从一个或多个基接口继承。
当基类型列表包含基类和接口时,基类必须是列表中的第一项。
实现接口的类可以显式实现该接口的成员。显式实现的成员不能通过类实例访问,而只能通过接口实例访问。
有关显式接口实现的更多详细信息和代码示例,请参见显式接口实现(C# 编程指南)。
下面的示例演示了接口实现。在此示例中,接口包含属性声明,类包含实现。
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
{{
// 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;
}
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
}
public int y
{
get
{
return _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# 参考)
- 接口(C# 参考)
- C#参考
- odata接口参考网站
- arcgis接口参考
- app接口文档(参考)
- JavaSE 学习参考:接口
- C#正则表达式参考
- [参考]C# 数据类型
- C#正则表达式参考
- C#语言参考
- C#参考书籍
- C#正则表达式参考
- C#语言参考视频
- C#正则表达式参考
- switch(C# 参考)
- base(C# 参考)
- abstract(C# 参考)
- 日常工作常用工具集
- 龙应台的演讲--百年思索
- C++库二进制兼容Binary Compatible教程
- 失恋的人看了这些电影也许可以快点好起来
- C++对象内存布局
- 接口(C# 参考)
- e-r概念模型
- 试玩drizzle
- 关于连接的认识
- 网线
- 【算法】归并--实现归并排序非递归算法
- 误差、权值、偏差的变化情况
- GCC 4.5.0 & GMP 5.0.1 的安装问题一例
- Microsoft SQL Server 2005 整合、集成SP3方法