| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4239 人关注过本帖
标题:[讨论]接口,回贴就给分
只看楼主 加入收藏
yushengou
Rank: 1
等 级:新手上路
帖 子:401
专家分:0
注 册:2005-3-30
收藏
得分:0 
接口就是两段独立代码之间的合约。
类由数据和在此数据上进行操作的方法组成。尽管类的方法表达了行为特征,但是按照更准确的分类,类被认为是东西,而不是行为,这就是需要接口的原因。接口使你能够定义行为特征或能力,并将这些行为应用于类,而不管类层次结构是什么样的。

是不是很难看懂啊

我是初学者,希望大家能多多帮助我 /bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor='hand'; this.alt='Click here to open new window\nCTRL+Mouse wheel to zoom in/out';}" onclick="if(!this.resized) {return true;} else {window.open('http://bbs./bbs/showimg.asp?BoardID=34&filename=2005-4/200542294030151.gif');}" onmousewheel="return imgzoom(this);" alt="" />
2005-04-09 09:19
shevchenkoka
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2005-3-27
收藏
得分:0 

备注 接口可以是命名空间或类的成员,并且可以包含下列成员的签名: 方法 属性 索引器 事件 一个接口可从一个或多个基接口继承。在下例中,接口 IMyInterface 从两个基接口 IBase1 和 IBase2 继承: interface IMyInterface: IBase1, IBase2 { void MethodA(); void MethodB(); } 接口可以由类和结构实现。实现的接口的标识符出现在类的基列表中。例如: class Class1: Iface1, Iface2 { // class members } 类的基列表同时包含基类和接口时,列表中首先出现的是基类。例如: class ClassA: BaseClass, Iface1, Iface2 { // class members } 有关接口的更多信息,请参见接口。 有关属性和索引器的更多信息,请参见属性声明和索引器声明。 示例 下例说明了接口的实现。在此例中,接口 IPoint 包含属性声明,后者负责设置和获取字段的值。MyPoint 类包含属性实现。 // keyword_interface.cs // Interface implementation using System; interface IPoint { // Property signatures: int x { get; set; }

int y { get; set; } }

class MyPoint : IPoint { // Fields: private int myX; private int myY;

// Constructor: public MyPoint(int x, int y) { myX = x; myY = y; }

// Property implementation: public int x { get { return myX; }

set { myX = value; } }

public int y { get { return myY; } set { myY = value; } } }

class MainClass { private static void PrintPoint(IPoint p) { Console.WriteLine("x={0}, y={1}", p.x, p.y); }

public static void Main() { MyPoint p = new MyPoint(2,3); Console.Write("My Point: "); PrintPoint(p); } } 输出 My Point: x=2, y=3

2005-04-09 11:42
shevchenkoka
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2005-3-27
收藏
得分:0 

实现接口的类可以显式实现该接口的成员。当显式实现某成员时,不能通过类实例访问该成员,而只能通过该接口的实例访问该成员。本教程包含两个示例。第一个示例阐释如何显式实现和访问接口成员。第二个示例展示如何实现具有相同成员名的两个接口。

示例 1

本示例声明一个 IDimensions 接口和一个 Box 类,该类显式实现接口成员 LengthWidth。通过接口实例 myDimensions 访问这些成员。

// explicit1.csinterface IDimensions {   float Length();   float Width();}class Box : IDimensions {   float lengthInches;   float widthInches;   public Box(float length, float width)    {      lengthInches = length;      widthInches = width;   }   // Explicit interface member implementation:    float IDimensions.Length()    {      return lengthInches;   }   // Explicit interface member implementation:   float IDimensions.Width()    {      return widthInches;         }   public static void Main()    {      // Declare a class instance "myBox":      Box myBox = new Box(30.0f, 20.0f);      // Declare an interface instance "myDimensions":      IDimensions myDimensions = (IDimensions) myBox;      // Print out the dimensions of the box:      /* The following commented lines would produce compilation          errors because they try to access an explicitly implemented         interface member from a class instance:                   */      //System.Console.WriteLine("Length: {0}", myBox.Length());      //System.Console.WriteLine("Width: {0}", myBox.Width());      /* Print out the dimensions of the box by calling the methods          from an instance of the interface:                         */      System.Console.WriteLine("Length: {0}", myDimensions.Length());      System.Console.WriteLine("Width: {0}", myDimensions.Width());   }}

2005-04-09 11:43
sportboy
Rank: 1
等 级:新手上路
帖 子:88
专家分:0
注 册:2005-3-24
收藏
得分:0 
版主呀!其实你说的和我们现在学的课本上说的差不多, 我还是不懂,不过就我理解,接口的用处好像就是XML中的DTD一样, 能在外部引用它!只是一套规则的集合,不过我就只能这么理解了, 你还有什么方法使我对他有更深的理解吗?希望回复!! 这不算灌水吧!如果是,那么请原谅我这个新手!

2005-04-11 15:55
jacky
Rank: 1
等 级:新手上路
帖 子:104
专家分:0
注 册:2004-12-17
收藏
得分:0 
接口很像一个抽象类
对吧
2005-04-11 15:57
jacky
Rank: 1
等 级:新手上路
帖 子:104
专家分:0
注 册:2004-12-17
收藏
得分:0 
322的也在
2005-04-11 15:58
幻风幻云
Rank: 1
等 级:新手上路
帖 子:762
专家分:0
注 册:2005-1-14
收藏
得分:0 
以下是引用sportboy在2005-4-11 15:55:44的发言: 版主呀!其实你说的和我们现在学的课本上说的差不多, 我还是不懂,不过就我理解,接口的用处好像就是XML中的DTD一样, 能在外部引用它!只是一套规则的集合,不过我就只能这么理解了, 你还有什么方法使我对他有更深的理解吗?希望回复!! 这不算灌水吧!如果是,那么请原谅我这个新手!
不懂XML阿 我开这个贴目的就是为了让大家讨论,因为我也不是很清楚,有什么见解都说出来,这样才能加深理解阿

2005-04-11 16:25
shevchenkoka
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2005-3-27
收藏
得分:0 
o
2005-04-11 17:05
yichen
Rank: 1
等 级:新手上路
帖 子:303
专家分:0
注 册:2005-3-9
收藏
得分:0 

接口可以有静态成员、嵌套类型、抽象、虚拟成员、属性和事件。实现接口的任何类都必须提供接口中所声明的抽象成员的定义。接口可以要求任何实现类必须实现一个或多个其他接口。

对接口有以下限制:

  • 接口可以用任何可访问性来声明,但接口成员必须全都具有公共可访问性。
  • 不能向成员或接口自身附加安全性权限
  • 接口可以定义类构造函数,但不能定义实例构造函数。

每种语言都必须为需要成员的接口映射一个实现提供规则,因为不只一个接口可以用相同的签名声明成员,且这些成员可以有单独的实现。


衣带渐宽终不悔, 为伊消得人憔悴。 纸上得来终觉浅, 绝知此事要躬行。
2005-04-11 22:34
shanji
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2005-1-23
收藏
得分:0 
2005-04-15 00:12
快速回复:[讨论]接口,回贴就给分
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012966 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved