接口使得实现接口的类或结构在形式上保持一致。
以上代码定义了接口IMyInterface。通常接口命令以I字母开头,这个接口只有一个方法MethodToImplement()。
interface IMyInterface
{
// 接口成员
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
}
public void MethodToImplement()
{
Console.WriteLine('MethodToImplement() called.');
}
}
接口的定义是指定一组函数成员而不实现成员的引用类型,其它类型和接口可以继承接口。定义还是很好理解的,但是没有反映特点,接口主要有以下特点:
以下实例定义了两个接口IMyInterface和IParentInterface
interface IParentInterface
{
void ParentInterfaceMethod();
}
interface IMyInterface : IParentInterface
{
void MethodToImplement();
}
class InterfaceImplementer : IMyInterface
{
static void Main()
{
InterfaceImplementer iImp = new InterfaceImplementer();
iImp.MethodToImplement();
iImp.ParentInterfaceMethod();
}
public void MethodToImplement()
{
Console.WriteLine('MethodToImplement() called.');
}
public void ParentInterfaceMethod()
{
Console.WriteLine('ParentInterfaceMethod() called.');
}
}
抽象类不能直接实例化,但允许派生出具体的,具有实际功能的类。
接口方法不能用publicabstract等修饰。接口内不能有字段变量,构造函数。接口内可以定义属性。如stringcolor{get;set;}这种。实现接口时,必须和接口的格式一致。必须实现接口的所有方法。
接口本身并不实现任何功能,它只是和声明实现该接口的对象订立一个必须实现哪些行为的契约。
接口中定义事件,索引器,属性,方法:
namespace ConsoleApplication1
{
delegate void Delegate();
interface I
{
event Delegate DE;
T this[int i]
{
get;
set;
}
List age
{
get;
set;
}
void Say();
}
class c : I
{
public event Delegate DE;
public c this[int i]
{
get { return age[i]; }
set { DE(); Say(); age[i] = value; }
}
public List age
{
get;
set;
}
public void Say()
{
Console.WriteLine('123');
}
}
class b : I
{
public event Delegate DE;
public c this[int i]
{
get { return age[i]; }
set { DE();Say(); age[i] = value; }
}
public List age
{
get;
set;
}
public void Say()
{
Console.WriteLine('456');
}
}
class Program
{
static void Main(string[] args)
{
I Ic = new c();
I lb = new b();
Ic.DE += deC;
lb.DE += deB;
lb = Ic;
lb.age = new List();
lb.age.Add(new c());
lb[0] = new c();
Console.WriteLine('S');
Console.ReadKey();
}
static void deC()
{
Console.WriteLine('c');
}
static void deB()
{
Console.WriteLine('B');
}
}
}
接口成员被定义为公共的,但抽象类的成员也可以是私有的、受保护的、内部的或受保护的内部成员。
接口注意的几点:
抽象类可以提供某些方法的部分实现,接口不可以。抽象类的实例是它的子类给出的。接口的实例是实现接口的类给出的。
接口用于规范,抽象类用于共性。抽象类是类,所以只能被单继承,但是接口却可以一次实现多个。
接口和抽象类的区别
在抽象类中加入一个方法,那么它的子类就同时有了这个方法。而在接口中加入新的方法,那么实现它的类就要重新编写。
接口使用interface关键字声明,它与类的声明类似。接口声明默认是public的。下面是一个接口声明的实例:
通过接口可以实现多重继承,C#接口的成员不能有public、protected、internal、private等修饰符。原因很简单,接口里面的方法都需要由外面接口实现去实现方法体,那么其修饰符必然是public。C#接口中的成员默认是public的。接口成员不能有new、static、abstract、override、virtual修饰符。有一点要注意,当一个接口实现一个接口,这2个接口中有相同的方法时,可用new关键字隐藏父接口中的方法。接口中只包含成员的签名,接口没有构造函数,所以不能直接使用new对接口进行实例化。接口中只能包含方法、属性、事件和索引的组合。接口一旦被实现,实现类必须实现接口中的所有成员,除非实现类本身是抽象类。C#是单继承,接口是解决C#里面类可以同时继承多个基类的问题。
class Program
{
static void Main(string[] args)
{
IWorker james1 = new James1();
IWorker james2 = new James2();
james1.work('设计');
james2.work('编程');
//从这个例子我体会到了有接口的好处,可以想象如果又来了新的员工。
//如果不采用接口,而是每个员工都有一个单独的类,这样就会容易出错。
//如果有接口这种协议约束的话,那么只要实现了接口就肯定有接口里声明的方法,我们只需拿来调用。
}
}
public interface IWorker{ void work(string s); }
class James1 : IWorker
{
public void work(string s)
{
Console.WriteLine('我的名字是James1,我的工作是' +s);
}
}
class James2 : IWorker
{
public void work(string s)
{
Console.WriteLine('我的名字是James2,我的工作是'+s);
}
}
抽象类在某种程度上与接口类似,它们大多只是用在当只有少数方法由基类声明由派生类实现时。
接口中只能声明方法,属性,事件,索引器。而抽象类中可以有方法的实现,也可以定义非静态的类变量。
文章为作者独立观点,不代表股票交易接口观点