Example.cs
namespace InterfaceDemo
{
#region Example #1
public interface IWriteTool
{
void Write();
}
public class Pen : IWriteTool
{
public void Write()
{
Console.WriteLine("This is Pen");
}
// 其他钢笔的属性和行为
}
public class Pencil : IWriteTool
{
public void Write()
{
Console.WriteLine("This is Pencil");
}
// 其他铅笔笔的属性和行为
}
public class Student
{
// 我们传入的参数是“笔”这个规范类型,而不是具体的钢笔类型或铅笔类型
public void Writing(IWriteTool tool)
{
tool.Write();
}
}
#endregion
#region Example #2
public interface IBark { void Bark();}
public class Dog : IBark
{
public Dog() { }
public void Bark()
{
Console.WriteLine("汪汪");
}
}
public class Cat : IBark
{
public Cat()
{
}
public void Bark()
{
Console.WriteLine("喵喵");
}
}
#endregion
}
Implementation.cs
namespace InterfaceDemo
{
#region 显示 实现
//如果类实现两个接口,并且这两个接口包含具有相同签名的成员,那么在类中实现该成员将导致 两个接口都使用该成员作为它们的实现
interface IControl
{
void Paint();
}
interface ISurface
{
void Paint();
}
//class SampleClass : IControl, ISurface
//{
// // Both ISurface.Paint and IControl.Paint call this method.
// public void Paint()
// {
// }
//}
//然而,如果两个接口成员执行不同的函数,那么这可能会导致其中一个接口的实现不正确或两个接口的实现都不正确。
//可以显式地实现接口成员 -- 即创建一个仅通过该接口调用并且特定于该接口的类成员。这是使用接口名称和一个句点命名该类成员来实现的
public class SampleClass : IControl, ISurface
{
void IControl.Paint()
{
System.Console.WriteLine("IControl.Paint");
}
void ISurface.Paint()
{
System.Console.WriteLine("ISurface.Paint");
}
}
//具有相同名称的不同成员(如属性和方法)
interface ILeft
{
int P { get; }
}
interface IRight
{
int P();
}
class Middle : ILeft, IRight
{
public int P() { return 0; }
int ILeft.P { get { return 0; } }
}
#endregion
#region 属性实现
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;
}
}
}
#endregion
#region 显示/隐式
// Declare the English units interface:
interface IEnglishDimensions
{
float Length();
float Width();
}
// Declare the metric units interface:
interface IMetricDimensions
{
float Length();
float Width();
}
class Box : IEnglishDimensions, IMetricDimensions
{
float lengthInches;
float widthInches;
public Box(float length, float width)
{
lengthInches = length;
widthInches = width;
}
// Normal implementation:
public float Length()
{
return lengthInches;
}
public float Width()
{
return widthInches;
}
// Explicit implementation:
float IMetricDimensions.Length()
{
return lengthInches * 2.54f;
}
float IMetricDimensions.Width()
{
return widthInches * 2.54f;
}
}
#endregion
}
namespace InterfaceDemo
{
class Program
{
static void Main(string[] args)
{
// Example #1
//Pen pen = new Pen();
//Pencil pencil = new Pencil();
//Student student = new Student();
//// 这里会有一个自动转型的过程,
//// 会自动将pen和pencil向上转换成“笔”来符合 “写作文(笔 tool)” 这个方法的参数规定
//student.Writing(pen); // 学生用钢笔写作文
//student.Writing(pencil); // 学生用铅笔写作文
////Exmple #2
//Dog 旺财 = new Dog();
//旺财.Bark();
//Cat 咪咪 = new Cat();
//咪咪.Bark();
// 显示实现 #3
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.
// 属性实现 #4
Point p = new Point(2, 3);
Console.Write("My Point: ");
Console.WriteLine("x={0}, y={1}", p.x, p.y);
//显示/隐式 #5
//类实例访问英制单位,而从接口实例访问公制单位
Box box1 = new Box(30.0f, 20.0f);
IMetricDimensions mDimensions = (IMetricDimensions)box1;
System.Console.WriteLine("Length(in): {0}", box1.Length());
System.Console.WriteLine("Width (in): {0}", box1.Width());
System.Console.WriteLine("Length(cm): {0}", mDimensions.Length());
System.Console.WriteLine("Width (cm): {0}", mDimensions.Width());
Console.ReadLine();
}
}
}