什么是接口
接口是指定一组函数成员而不实现他们的引用类型。
所以只能类和结构实现接口。
例:用接口使PrintInfo方法能够用于多个类
interface IInfo
{
string GetName();
string GetAge();
}
class CA : IInfo
{
public string Name;
public int Age;
public string GetName()
{
return Name;
}
public string GetAge()
{
return Age.ToString();
}
}
class CB : IInfo
{
public string First;
public string Last;
public double PersonsAge;
public string GetName()
{
return First + "" + Last;
}
public string GetAge()
{
return PersonsAge.ToString();
}
}
class Program
{
static void PrintInfo(IInfo item)
{
Console.WriteLine("Name:{0}.Age{1}",item .GetName() ,item .GetAge() );
}
static void Main(string[] args)
{
CA a = new CA() { Name = "John Doe", Age = 18 };
CB b = new CB() { First = "Jane", Last = "Doe", PersonsAge = 19 };
PrintInfo(a);
PrintInfo(b);
Console.ReadKey();
}
}
声明接口
interface ImyInterfacel//关键字+接口名称
{
string GetName();//分号代替了主体
int GetAge();
}
接口声明可以有任何的访问修饰符public、protected、internal或private
接口的成员的隐式public的,不允许有任何的访问修饰符,包括public
实现接口
只有类和结构才能实现接口
在基类列表中包括接口名称;
为每一个接口的成员提供实现。
class MyClass : IComparable// 类名:接口名
{
int Dostuff(int nVar1 ,long 1var2)
{。。。。。}//实现代码
double DoOtherStuff(string s,long x)
{。。。。。}//实现代码
}
如果类实现了接口,它必须实现接口的所有成员
如果类从基类继承并实现了接口,基类列表中的基类名称必须放在所有接口之前。
简单接口的示例
interface IIfc1
{
void PrintOut(string s);//分号代替了主体
}
class MyClass : IIfc1//实现接口 声明类
{
public void PrintOut(string s) //实现
{
Console.WriteLine("Calling through:{0}",s);
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.PrintOut("object");
}
}
接口是引用类型
IIfc1 ifc = (IIfc1)mc;
接口 接口引用=(转换为接口)类对象引用
static void Main(string[] args)
{
MyClass mc = new MyClass();//创建类对象
mc.PrintOut("object");//调用类对象的实现方法
IIfc1 ifc = (IIfc1)mc;//将类对象的引用转换为接口类型的引用
ifc.PrintOut("interface");//调用引用方法
ILiveBirth b=a as ILiveBirth;
b是接口引用
a是类对象引用
ILiveBirth接口名
实现多个接口
interface IDataRetrieve//声明接口
{
int GetData();
}
interface IDatastore//声明接口
{
void SetData(int x);
}
class MyData : IDataRetrieve, IDatastore//声明类
{
int Meml;//声明字段
public int GetData()
{
return Meml;
}
public void SetData(int x)
{
Meml = x;
}
}
class Program
{
static void Main(string[] args)
{
MyData data = new MyData();
data.SetData(5);
Console.WriteLine("Value={0}",data.GetData());
}
实现具有重复成员的接口
如果一个类实现了多个接口,并且其中一些接口有相同签名和返回类型的成员,那么类可以实现单个成员来满足所有包含重复成员的接口。
interface IIfc1
{
void PrintOut(string s);
}
interface IIfc2
{
void PrintOut(string t);
}
class MyClass : IIfc1, IIfc2
{
public void PrintOut(string s)
{
Console.WriteLine("Calling through:{0}",s);
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
mc.PrintOut("object");
}
}
多个接口的引用
如果类实现了多个接口,我们可以获取每一个接口的独立引用
interface IIfc1
{
void PrintOut(string s);
}
interface IIfc2
{
void PrintOut(string t);
}
class MyClass : IIfc1, IIfc2
{
public void PrintOut(string s)
{
Console.WriteLine("Calling through:{0}", s);
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();
IIfc1 ifc1 = (IIfc1)mc;//获取IIfc1的引用
IIfc2 ifc2 = (IIfc2)mc;//获取IIfc2的引用
mc.PrintOut("object");//从类对象引用
ifc1.PrintOut("interface 1");//从IIfc1调用
ifc2.PrintOut("interface 2");//从IIfc2调用
}
}
派生成员作为实现
实现接口的类可以从它的基类继承实现代码
interface IIfc1
{
void PrintOut(string s);
}
class MyBaseClass//声明基类
{
public void PrintOut(string s)//声明方法
{
Console.WriteLine("Calling through:{0}", s);
}
}
class Derived : MyBaseClass, IIfc1//声明类
{ }
class Program
{
static void Main(string[] args)
{
Derived d = new Derived();//创建类对象
d.PrintOut("object");//调用方法
}
}
显示接口分离实现
创建显示接口成员实现,特性
1.与所有接口实现相似,位于实现了接口的类或结果中。
2.它使用限定接口名称来声明,由接口名称和成员名称以及他们中间的点分割符号构成
interface IIfc1
{
void PrintOut(string s);
}
interface IIfc2
{
void PrintOut(string t);
}
class MyClass : IIfc1, IIfc2
{
void IIfc1.PrintOut(string s)//显示接口成员实现
{
Console.WriteLine("Calling through:{0}", s);
}
void IIfc2.PrintOut(string t)//显示接口成员实现
{
Console.WriteLine("Calling through:{0}", t);
}
}
class Program
{
static void Main(string[] args)
{
MyClass mc = new MyClass();//创建类对象
IIfc1 ifc1=(IIfc1)mc ;//获取IIfc1的引用
ifc1.PrintOut("interface 1");//调用显示实现
IIfc2 ifc2 = (IIfc2)mc;//获取IIfc2的引用
ifc1.PrintOut("interface 2");//调用显示实现
}
显示实现满足了类或结构必须实现的方法
访问显示接口成员实现
显示接口成员实现只可以通过指向接口的引用来访问。
这个限制对继承产生了重要的影响。由于其他类成员不能直接访问显示接口成员实现,衍生类的成员也不能直接访问他们。
他们必须总是通过接口的引用来访问。
接口可以继承接口
接口可以在基接口列表中有任意多个接口
interface IDataRetrieve
{
int GetData();
}
interface IDatastore
{
void SetData(int x);
}
//从前两个接口继承
interface IDataIO:IDataRetrieve,IDatastore
{
}
class MyData : IDataIO
{
int nPrivateData;
public int GetData()
{
return nPrivateData;
}
public void SetData(int x)
{
nPrivateData = x;
}
}
class Program
{
static void Main(string[] args)
{
MyData data=new MyData();
data.SetData(5);
Console.WriteLine("{0}",data.GetData());
}
}
不同类实现一个接口示例
interface ILiveBirth//声明接口
{
string BabyCalled();
}
class Animal { }//
class Cat : Animal, ILiveBirth
{
string ILiveBirth.BabyCalled()
{ return "cat"; }
}
class Dog : Animal, ILiveBirth
{
string ILiveBirth.BabyCalled()
{ return "dog"; }
}
class bird : Animal { }
class Program
{
static void Main(string[] args)
{
Animal[] animalArray=new Animal[3];
animalArray [0] = new Cat();
animalArray [1] = new Dog();
animalArray [2] = new bird();
foreach (Animal a in animalArray)
{
ILiveBirth b = a as ILiveBirth;
if (b != null)
{
Console.WriteLine("Baby is called:{0}", b.BabyCalled());
}
else
{
Console.WriteLine("Baby is called:bird");
}
}
}
}