using System.IO; using System; namespace WTest { public interface IAnimal { string Name { get; set; } void Smile(); void Cry(); } public class Cat : IAnimal { public string Name { get; set; } public void Smile() { Console.Write("Cat Smile"); } public void Cry() { Console.Write("Cat Cry"); } } public class Dog : IAnimal { public string Name { get; set; } public void Smile() { Console.Write("Dog Smile"); } public void Cry() { Console.Write("Dog Cry"); } } } 用法 IAnimal c = new Cat() as IAnimal; IAnimal d = new Dog() as IAnimal; c.Smile(); c.Cry(); d.Smile(); d.Cry();
经典例子: using 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); } } 输出 My Point: x=2, y=3
using System.IO;
using System;
namespace WTest
{
public interface IAnimal
{
string Name { get; set; }
void Smile();
void Cry();
} public class Cat : IAnimal
{
public string Name { get; set; }
public void Smile()
{
Console.Write("Cat Smile");
} public void Cry()
{
Console.Write("Cat Cry");
}
} public class Dog : IAnimal
{
public string Name { get; set; }
public void Smile()
{
Console.Write("Dog Smile");
} public void Cry()
{
Console.Write("Dog Cry");
}
}
}
用法 IAnimal c = new Cat() as IAnimal;
IAnimal d = new Dog() as IAnimal; c.Smile();
c.Cry();
d.Smile();
d.Cry();
Cat和Dog两个类实现了这两个接口。
具体的使用方法是比较简单的……
举个例子:某公司有一台特殊打印机,还可以使用一年,一年后可能换为另一种打印机,这两种打印机都特殊而贵.所以现在的程序希望换了打印机后也少量修改就可用.
方法:
1,定义一个打印机接口.
2,定义打印机类A,B,分别实现此接口.
3,定义一个工厂类,在类中可选择返回由A实现的接口,或者由B实现的接口.
4,在程序中使用打印机时,就可以使用工厂类来调用打印机,而不需要知道具体的是什么打印机.
如果打印机换了,只需要修改工厂类就行了.如果有一千个地方都调用过打印机,就不需要一个一个修改.修改一个地方就行了.接口充当一个隔离层的作用.
*/
//定义打印机接口
interface Iprint
{
bool PrintData(string data);
}//定义打印机类A,实现接口,(继承)class PrintA:Iprint
{
public virtual bool PrintData(string data)
{
//具体业务逻辑略
}
} 定义打印机类B,实现接口,(继承)class PrintB:Iprint
{
public virtual bool PrintData(string data)
{
//具体业务逻辑略
}
}//定义工厂类
class PrintFactory{
public Iprint CreatePrint()
{
//返回一个由打机类A,或B实现的接口,比如
return new PrintA();
}
}//通过工厂类,调用打印机
private void button1_Click(object sender,EventArgs e)
{
PrintFactory myFactory=new PrintFactory();
Iprint myPrint=myFactory.CreatePrint();
myPrint.PrintData("这样做很方便啊");
}
using System;
using System.Collections.Generic;
using System.Text;namespace 例7接口
{
interface IA { void f(int i);} //定义接口
interface IB { void g(double d);}
class C : IA, IB //c继承与接口IA,IB
{
public void f(int i) { Console.WriteLine("C.f({0})", i); } //对IA里f()的实现
public void g(double d) { Console.WriteLine("C.g({0})", d); }
} class Program
{
static void Main(string[] args)
{
IA obj1 = new C(); //声明接口obj1,它是C类型的,这点可能不好理解,多看点资料
IB obj2 = new C();
C _obj2 = (C)obj2;
obj1.f(5);
Console.ReadLine(); }
}
}
How to: Explicitly Implement Interface Members with Inheritance (C# Programming Guide)
http://msdn.microsoft.com/en-us/library/4taxa8t2.aspx
不需要全部看完,就从开始看,,第一个例子看完就可以了,应该会对接口有个更深的认识.
如果我没记错,是一个"鸭子游戏"的例子.
using 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);
}
} 输出
My Point: x=2, y=3
给个连接:http://topic.csdn.net/u/20080806/13/cce5d3ec-8b56-4cbc-875c-f805f8ce07f5.html
其中18楼的连接不错,符合简单实用