| 1. --接口描述的是可属于任何类或结构的一组相关功能。 --接口没有字段,没有构造函数(由方法、属性、事件、索引器或这四种成员类型的任意组合构成) --接口没有实现 --不能直接实例化接口 --接口可以继承其他多个接口,类和结构可从多个接口继承。 --若要实现接口成员,类中的对应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名。 --继承接口的任何非抽象类型都必须实现接口的所有成员 --类或结构继承接口与类继承基类或结构的区别:可继承多个接口。仅继承方法名称和签名 2.复制代码interface IEquatable<Car>
{
    bool Equals(Car obj);
}
public class Car : IEquatable<Car>
{
    public string Make {get; set;}
    public string Model { get; set; }
    public string Year { get; set; }
 
    // Implementation of IEquatable<T> interface
<font color="#ff0000">    public</font> bool Equals(Car car)
    {
        if (this.Make == car.Make &&
            this.Model == car.Model &&
            this.Year == car.Year)
        {
            return true;
        }
        else
            return false;
    }
}
class test
{
       static void Main()
       {
              Car car1=new Car();
              Car car2=new Car();
              if(car1.Equals(car2))
              {
                     System.Console.WriteLine("equal");
              }
              else
              {
                     System.Console.WriteLine("not equal");
              }
       }
}
/*
equal
*/
 
类的属性和索引器可以为接口上定义的属性或索引器定义额外的访问器。例如,接口可以声明一个带有 get 访问器的属性,而实现该接口的类可以声明同时带有 get 和 set 访问器的同一属性。但是,如果属性或索引器使用显式实现,则访问器必须匹配。3. 类可以通过其继承的基类或接口多次继承某个接口。在这种情况下,如果将该接口声明为新类的一部分,则该类只能实现该接口一次。如果没有将继承的接口声明为新类的一部分,其实现将由声明它的基类提供。基类可以使用虚拟成员实现接口成员;在这种情况下,继承接口的类可通过重写虚拟成员来更改接口行为4. 语法分析: 情况一:两个接口包含具有相同签名的成员 复制代码interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}
结果:在类中实现该成员将导致两个接口都使用该成员作为它们的实现 复制代码interface IControl
{
    void Paint();
}
interface ISurface
{
    void Paint();
}
class SampleClass : IControl, ISurface
{
    // Both ISurface.Paint and IControl.Paint call this method.
<font color="#ff0000">    public</font> void Paint()
    {
    }
}
  情况二:两个接口分别声明具有相同名称的不同成员(如属性和方法) 复制代码interface ILeft
{
    int P { get;}
}
interface IRight
{
    int P();
}
结果:在类中实现该成员将导致编译器错误   解决办法:可以显式地实现接口成员(即创建一个仅通过该接口调用并且特定于该接口的类成员) 针对情况一:(方法有多种) 复制代码public class SampleClass : IControl, ISurface
{
    void IControl.Paint()//没有public
    {
        System.Console.WriteLine("IControl.Paint");
    }
    void ISurface.Paint()//没有public
    {
        System.Console.WriteLine("ISurface.Paint");
    }
}
类成员 IControl.Paint 只能通过 IControl 接口使用,ISurface.Paint 只能通过 ISurface 使用。两个方法实现都是分离的,都不可以直接在类中使用。 复制代码SampleClass obj = new SampleClass();
//obj.Paint();  // Compiler error.
IControl c = (IControl)obj;
c.Paint();  // Calls IControl.Paint on SampleClass.
ISurface s = (ISurface)obj;
s.Paint(); // Calls ISurface.Paint on SampleClass.
针对情况二:为了同时实现两个接口,类必须对属性 P 和/或方法 P 使用显式实现(方法有多种) 复制代码class Middle : ILeft, IRight
{
<font color="#ff0000">    public</font> int P() { return 0; }
<font color="#ff0000">    int ILeft.P</font> { get { return 0; } }//没有public
}
使用办法与上面类似   实际应用:   例:同时以公制单位和英制单位显示框的尺寸。Box 类实现 IEnglishDimensions 和 IMetricDimensions 两个接口,它们表示不同的度量系统。两个接口有相同的成员名称 Length 和 Width。   复制代码// Declare the English units interface:
interface IEnglishDimensions
{
    float Length();
    float Width();
}
 
// Declare the metric units interface:
interface IMetricDimensions
{
    float Length();
    float Width();
}
 
// Declare the Box class that implements the two interfaces:
// IEnglishDimensions and IMetricDimensions:
class Box : IEnglishDimensions, IMetricDimensions
{
    float lengthInches;
    float widthInches;
 
    public Box(float length, float width)
    {
        lengthInches = length;
        widthInches = width;
    }
 
    // Explicitly implement the members of IEnglishDimensions:
    float IEnglishDimensions.Length()
    {
        return lengthInches;
    }
 
    float IEnglishDimensions.Width()
    {
        return widthInches;
    }
 
    // Explicitly implement the members of IMetricDimensions:
    float IMetricDimensions.Length()
    {
        return lengthInches * 2.54f;
    }
 
    float IMetricDimensions.Width()
    {
        return widthInches * 2.54f;
    }
 
    static void Main()
    {
        // Declare a class instance box1:
        Box box1 = new Box(30.0f, 20.0f);
 
        // Declare an instance of the English units interface:
        IEnglishDimensions eDimensions = (IEnglishDimensions)box1;
 
        // Declare an instance of the metric units interface:
        IMetricDimensions mDimensions = (IMetricDimensions)box1;
 
        // Print dimensions in English units:
        System.Console.WriteLine("Length(in): {0}", <font color="#ff0000">eDimensions.Length()</font>);
        System.Console.WriteLine("Width (in): {0}", <font color="#ff0000">eDimensions.Width()</font>);
 
        // Print dimensions in metric units:
        System.Console.WriteLine("Length(cm): {0}", <font color="#ff0000">mDimensions.Length()</font>);
        System.Console.WriteLine("Width (cm): {0}", <font color="#ff0000">mDimensions.Width()</font>);
    }
}
/* Output:
    Length(in): 30
    Width (in): 20
    Length(cm): 76.2
    Width (cm): 50.8
*/
 
都只能通过接口访问,不能直接在类中使用   如果希望默认度量采用英制单位,则正常实现 Length 和 Width 这两个方法,并从 IMetricDimensions 接口显式实现 Length 和 Width 方法: 复制代码// Normal implementation:
<font color="#ff0000">public</font> float Length()
{
    return lengthInches;
}
<font color="#ff0000">public</font> float Width()
{
    return widthInches;
}
 
// Explicit implementation:
float IMetricDimensions.Length()
{
    return lengthInches * 2.54f;
}
float IMetricDimensions.Width()
{
    return widthInches * 2.54f;
}
这种情况下,可以从类实例访问英制单位,而从接口实例访问公制单位: 复制代码public static void Test()
{
    Box box1 = new Box(30.0f, 20.0f);
    IMetricDimensions mDimensions = (IMetricDimensions)box1;
 
    System.Console.WriteLine("Length(in): {0}", <font color="#ff0000">box1.Length()</font>);
    System.Console.WriteLine("Width (in): {0}", <font color="#ff0000">box1.Width()</font>);
    System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
    System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
}
 |