0
点赞
收藏
分享

微信扫一扫

CSharp(C#)语言_第十三章(接口)


接口

  • ​​13.1 什么是接口​​
  • ​​13.2 声明接口:​​
  • ​​13.3 实现接口​​
  • ​​13.4 接口是引用类型​​
  • ​​13.5 实现多个接口​​
  • ​​13.6 实现具有重复成员的接口​​
  • ​​13.7 多个接口的引用​​
  • ​​13.8 派生成员作为实现​​
  • ​​13.9 显式接口成员实现​​
  • ​​13.10 接口可以继承接口​​
  • ​​13.11 不同类实现同一个接口的示例​​

13.1 什么是接口

  接口是指定一组函数成员而不实现它们的引用类型,所以只能类和结构来实现接口

13.2 声明接口:

接口的声明不能包含以下成员:

  • 数据成员
  • 静态成员

接口的声明只能包含如下类型的非静态成员函数的声明:

  • 方法
  • 属性
  • 事件
  • 索引器

注意:

  • 这些函数的声明不能包含任何实现代码,而在每一个成员声明的主体后必须使用分号
  • 按照惯例,接口名必须从大写的 I 开始(如:ISaveable)
  • 与类和结构一样,接口声明还可以分隔成分部接口声明
    eg:

// 关键字    接口名
interface IMyInterface1
{
int DoStuff(int nVar1, long lVar2);
double DoOtherStuff(string s, long x);
}

  • 接口声明可以有任何访问的修饰符 ​​public 、protected 、internal 或 private​
  • 接口的成员是隐式 public 的,不允许有任何访问修饰符,包括 public

13.3 实现接口

ps:实现 IMyInterface1 接口

// 关键字    接口名
class MyClass:IMyInterface1
{
int DoStuff(int nVar1, long lVar2)
{
...
//实现代码
}
double DoOtherStuff(string s, long x)
{
...
//实现代码
}
}

下面实现一个简单的接口

//声明接口
interface IIfc1
{
void PrintOut(string s);
}

// 实现接口
class MyClass:IIfc1 // 声明类
{
// 实现
public void PrintOut(string s)
{
Console.Mriteline("Calling through:{0)",s);
}
}

class Program
{
static void Main()
{
// 创建实例
MyClass mc = new MyClass();
// 调用方法
mc.PrintOut("object");
}
}

// 输出: Calling through:object

13.4 接口是引用类型

  接口不仅仅是类或结构要实现的成员列表。它是一个引用类型

13.5 实现多个接口

// 声明接口
interface IDataRetrieve
{
int GetData();
}
interface IDataStore
{
void SetData( int x );
}

// 声明类
// 接口 接口
class MyData:IDataRetrieve,IDataStore
{
int Mem1; // 声明字段
public int GetData() {
return Mem1;
}
public void SetData( int x)
{
Mem1=x;
}
}

class Program
{
static void Main()
{
MyData data = new MyData();
data.SetData( 5);
Console.MriteLine("Value=(0)",data.GetData());
}
}

// Value = 5

13.6 实现具有重复成员的接口

// 声明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
interface IDataStore
{
void PrintOut(string t);
}

//实现两个接口
class MyClass:IIfc1,IIfc2
{
public void PtintOut(string s) // 两个接口的单一实现
{
Console.WriteLine("Calling through:{0}",s);
}
}

class Program
{
static void Main()
{
MyClass mc = new MyClass();
mc.PrintOut("Object");
}
}

// 输出: Calling through:Object

13.7 多个接口的引用

// 声明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
interface IDataStore
{
void PrintOut(string t);
}

//实现两个接口
class MyClass:IIfc1,IIfc2
{
public void PtintOut(string s) // 两个接口的单一实现
{
Console.WriteLine("Calling through:{0}",s);
}
}

class Program
{
static void Main()
{
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调用
}
}

13.8 派生成员作为实现

// 声明接口
interface IDataRetrieve
{
void PrintOut(string s);
}
//实现两个接口
class MyBaseClass
{
public void PtintOut(string s) // 实现方法
{
Console.WriteLine("Calling through:{0}",s);
}
}
//声明类
class Derived:MyClass,IIfc1 {}

class Program
{
static void Main()
{
Derived d = new Derived();// 创建类对象
d.PtintOut("object"); // 调用方法
}
}

13.9 显式接口成员实现

显式接口成员实现有如下特性:

  • 与所有接口实现相似,位于实现了接口的类或结构中
  • 它使用限定接口名称来声明,由接口名称和成员名称以及它们中间的点分隔符号构成

显式接口成员实现:

class MyClass:IIfc1,IIfc2
{
void IIfc1.PrintOut(string s)
{
...
}

void IIfc2.PrintOut(string s)
{
...
}
}

13.10 接口可以继承接口

  接口实现可以从基类被继承,而接口本身也可以从一个或多个接口继承

  • 要指定某个接口继承其他的接口,应在接口声明中把基接口以逗号分隔的列表形式放在接口名称的冒号后面

//       接口名      基接口列表
interface IDataIO : IDataRetrieve, IDataStore
{
...
}

  • 与类不同,它在基类列表中只能有一个类名,而接口可以在基接口列表中有任意多个
    接口
  • ■ 列表中的接口本身可以继承其他接口
  • ■ 结果接口包含它声明的所有接口和所有基接口的成员

interface IDataRetrieve
{
int GetData();
}
interface IDataStore
{
void SetData( intx);
}
// 从前两个接口继承
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()
{
MyData data • new MyData();
data.SetData(5);
Console.MriteLine("(0)", data.GetData());
}
}

13.11 不同类实现同一个接口的示例

interface ILiveBirth // 声明接口
{
string BabyCalled();
}

class Animal{} // 基类Animal

class Cat : Animal,ILiveBirth //声明Cat类
{
string ILiveBirth.BabyCalled()
{
return"kitten";
}
}

class Dog : Animal,ILiveBirth // 声明Dog类
{
string ILiveBirth.BabyCalled()
{
return"puppy";
}
}

class Bird : Animal{} //声明Bird类

class Program
{
static void Main()
{
Animal[] animalArray = new Animal[3]; // 创建Animal数姐
animalArray[o]= new Cat(); // 插入Cat类对象
animalArray[1] = new Bird(); // 插入Bird奏对象
animalArray[2] = new Dog(); // 插入Dog奏对象
foreach( Animal a in animalArray ) // 在数组中循环
{
ILiveBirth b = a as ILiveBirth; //如果实现ILiveBirth...
if (b != null)
Console.Mriteline("Baby is called:(0)", b.BabyCalled());
}
}
}


举报

相关推荐

0 条评论