当前位置:首页 > Windows程序 > 正文

对C#中的接口使用和理解 转

2021-05-24 Windows程序

接口就是定义一个协定,只是一个架子而已,必须由结构或者类来进行实现。

接口的定义和使用

例如:定义一个接口

interface Border  

{

int weight //没有实现

{

set;        

get;

}

int height //没有实现

{

set;

get;

}

}//什么都没有做,只告诉大家两个属性weight,height,要使用这个接口,必须对它进行实现。

class Square:Border //注意对接口的实现

{

private int x;

private int y;

public int weight

{

get

{

return x;

}

set

{

x = value;

}

}

public int height

{

get

{

return y;

}

set

{

y = value;

}

}

对接口的应用

Border b = new Square();

b.weight =2;

b.height = 2;

Console.WriteLine("heighe is {0,weight is {1}",b.height,b.weight);

上述简单对接口进行了一个举例。运行之后相信对接口应该有个简单的概念。

接口的注意事项

1 实现类必须对接口的所有成员进行实现,例如上例中的weight和height属性都要实现。

2 接口不能包括变量成员,只能是属性、事件、方法、索引器(上述例子只有两个属性)

3 实现接口的类必须严格按照接口的定义来进行实现

4 实现接口可以隐式和显式来实现

例如:

interface IControl //接口1

{

void Paint();

}

interface ISurface //接口 2

{

void Paint();

}

//注意:上述两个接口都包含方法Paint(),下面实现类中必须进行显式实现

class SampleClass : IControl, ISurface

{

Void Paint()//如此隐式实现肯定容易出现歧义

void IControl.Paint() //显式实现

{

System.Console.WriteLine("IControl.Paint");

}

void ISurface.Paint()//显式实现

{

System.Console.WriteLine("ISurface.Paint");

}

}

5 接口中的成员显式实现后,必须通过接口调用,而不能直接通过类进行调用

上述事例中 SampleClass sp = new SampleClass();

Sp.Paint()这样是错误的,必须IControl I1 = (IControl) sp;然后I1. Paint();

6 一个接口可以由几个类进行实现,也可以只有一个类进行实现,也可以叫做接口的多态

7 一个实现类可以同时实现几个接口,也可以实现唯一一个接口

8 已有实现类的接口定义后不能随意更改,否则容易打乱实现类。

完成接口练习的例子:

using System;

using System.Collections.Generic;

using System.Text;

interface Border //定义接口

{

int weight

{

set;

get;

}

int height

{

set;

get;

}

}

interface Area   //定义接口

{

int ReAreaValue();

}

interface Perimeter //定义接口

{

int RePerimeterValue();

}

interface px //定义接口

{

string pring();

}

interface IControl //定义接口

{

void Paint();

}

interface ISurface //定义接口

{

void Paint();

}

class Square:Border,Area,Perimeter //实现接口,同时实现Border,Area,Perimeter三个接口

{

private int x;

private int y;

public int weight

{

get

{

return x;

}

set

{

x = value;

}

}

public int height

{

get

{

return y;

}

set

{

y = value;

}

}

public int ReAreaValue()

{

return x * y;

}

public int RePerimeterValue()

{

return (x + y) * 2;

}

}

class tempclass : Area //实现接口,再次实现Area接口 [多态性]

{

public int x = 3;

public int ReAreaValue()

{

return x*x;

}

}

class SampleClass : IControl, ISurface //实现接口 IControl, ISurface

{

void IControl.Paint() //显示实现

{

System.Console.WriteLine("IControl.Paint");

}

void ISurface.Paint()//显示实现

{

System.Console.WriteLine("ISurface.Paint");

}

}

class MainClass

{

static void wrtieBorder(Border b)

{

Console.WriteLine("heighe is {0,weight is {1}",b.height,b.weight);

}

static void writeArea(Area a) 

{

Console.WriteLine("a.ReAreaValue() is {0}", a.ReAreaValue());

}

static void writePerimeter(Perimeter p)

{

Console.WriteLine("p.RePerimeterValue() is {0}", p.RePerimeterValue());

}

static void Main()

{

Square s1 = new Square();

s1.weight =2;

s1.height = 2;

wrtieBorder(s1);

writePerimeter(s1);

writeArea(s1);

tempclass temp = new tempclass();

Console.WriteLine("Main tempclass object temp.ReAreaValue() is " + temp.ReAreaValue());

writeArea(temp);

Border b = new Square();

b.weight = 5;

b.height = 6;

wrtieBorder(b);

SampleClass obj = new SampleClass();

IControl c = (IControl)obj;

c.Paint();

ISurface s = (ISurface)obj;

s.Paint(); // Calls ISurface.Paint on SampleClass.

Console.ReadLine();

}

}

接口与抽象类

接口与抽象类有很多相似的地方,也有区别的地方:如下

抽象类:一种不能实例化而必须从中继承的类,抽象类可以提供实现,也可以不提供实现

子类只能从一个抽象类继承

抽象类应主要用于关系密切的对象

如果要设计大的功能单元,则使用抽象类。

如果预计要创建组件的多个版本,则创建抽象类

接口:是完全抽象的成员集合,不提供认识实现。

类或者结构可以继承几个接口。

接口最适合为不相关的类提供通用功能

如果要设计小而简练的功能块,,则使用接口

 总结

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70103.html