本文共 5054 字,大约阅读时间需要 16 分钟。
一 接口简单理解就是一种约定,使得实现接口的类或结构在形式上保持一致。 声明接口在语法上和声明抽象类完全相同,例如声明一个银行账户的接口: public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } 接口中只能包含方法、属性、索引器和事件的声明。不允许声明成员上的修饰符,pubilc都不行,因为接口成员总是公有的,也不能声明为虚拟和静态的。如果需要修饰符,让实现类来声明。 1. 接口是一个引用类型,通过接口可以实现多重继承。 2. C#中接口的成员不能有new、public、protected、internal、private等修饰符。 3. 接口中只能声明"抽象"成员(所以不能直接下一步对接口进行实例化(即不能使用new操作符声明一个接口的实例对 象)),而不能声明共有的域或者私有的成员变量。 4. 接口声明不包括数据成员,只能包含方法、属性、事件、索引等成员。 5. 接口名称一般都以“I”作为首字母(当然不这样声明也可以),这也是接口和类的一个区别之一。 6. 接口成员的访问级别是默认的(默认为public),所以在声明时不能再为接口成员指定任何访问修饰符,否则 编译器会报错。 7. 接口成员不能有static、abstract、override、virtual修饰符,使用new修饰符不会报错,但会给出警告说不需要关键字new。 8. 在声明接口成员的时候,不准为接口成员编写具体的可执行代码,也就是说,只要在对接口进行声明时指明接口的成员名称和参数就可以了。 9. 接口一旦被实现,实现类必须实现接口中的所有成员,除非实现类本身是抽象类(通过具体的可执行代码实现接口抽象成员的操作)。 1、C#中的接口是独立于类来定义的。这与 C++模型是对立的,在 C++中接口实际上就是抽象基类。 2、接口和类都可以继承多个接口。 3、类可以继承一个基类,接口根本不能继承类。这种模型避免了 C++的多继承问题,C++中不同基类中的实现可能出现冲突。因此也不再需要诸如虚拟继承和显式作用域这类复杂机制。C#的简化接口模型有助于加快应用程序的开发。 4、一个接口定义一个只有抽象成员的引用类型。C#中一个接口实际所做的,仅仅只存在着方法标志,但根本就没有执行代码。这就暗示了不能实例化一个接口,只能实例化一个派生自该接口的对象。 5、接口可以定义方法、属性和索引。所以,对比一个类,接口的特殊性是:当定义一个类时,可以派生自多重接口,而你只能可以从仅有的一个类派生。 二 IBankAccount venusAccount = new SaverAccount(); IBankAccount jupiterAccount = new CurrentAccount(); 如上两句,把它们声明为IBankAccount引用的方式,而没有声明为类的引用。这样就可以让它指向执行这个接口的任何类的实例了,比较灵活。但这也有个缺点,如果我们要执行不属于接口的方法,比如这里重载的ToString()方法,就要先把接口的引用强制转换成合适的类型了。 三 接口的继承 接口也可以彼此继承,就象类的继承一样。比如我们又声明一个接口ITransferBankAccount,它继承于IBankAccount接口。
比如在这个接口中,又新增加了一个方法TransferTo(),所以如果要写一个类从ITransferBankAccount继承的话,就必须要实现IBankAccount和ITransferBankAccount两个接口所有的方法声明。
四 编程实例
1
using System;
namespace jiekoudemo1 { interface ISampleInterface { void SampleMethod(); } class ImplementationClass : ISampleInterface { // Explicit interface member implementation: void ISampleInterface.SampleMethod() { Console.WriteLine("我是方法实现"); } } class Program { public static void Main(string[] args) { // Declare an interface instance. ISampleInterface obj = new ImplementationClass(); // Call the member. obj.SampleMethod(); Console.Write("按任意键继续 . . . "); Console.ReadKey(true); } } }
2
using System;
namespace jiekoudemo2 { interface IPrint { void Print(); } class A : IPrint { public void Print() { System.Console.WriteLine("我是A"); } } class B : IPrint { public void Print() { System.Console.WriteLine("我是B"); } } class Program { public static void Print(IPrint obj) { obj.Print(); } public static void Main(string[] args) { Print(new A()); // display A Print(new B()); // display B Console.Write("按任意键继续 . . . "); Console.ReadKey(true); } } }
3
using System;
using System.Collections.Generic;using System.Text;interface ImyInterface { /// <summary> /// 编号(可读可写) /// </summary> string ID { get; set; } /// <summary> /// 姓名(可读可写) /// </summary> string Name { get; set; } /// <summary> /// 显示定义的编号和姓名 /// </summary> void ShowInfo(); } class Program:ImyInterface//继承自接口 { string id = ""; string name = ""; /// <summary> /// 编号 /// </summary> public string ID { get { return id; } set { id = value; } } /// <summary> /// 姓名 /// </summary> public string Name { get { return name; } set { name = value; } } /// <summary> /// 显示定义的编号和姓名 /// </summary> public void ShowInfo() { Console.WriteLine("编号\t 姓名"); Console.WriteLine(ID + "\t " + Name); } static void Main(string[] args) { Program program = new Program(); //实例化Program类对象 ImyInterface imyinterface = program; //使用派生类对象实例化接口ImyInterface imyinterface.ID = "TM"; //为派生类中的ID属性赋值 imyinterface.Name = "IT江湖"; //为派生类中的Name属性赋值 imyinterface.ShowInfo(); //调用派生类中方法显示定义的属性值 Console.Write("按任意键继续 . . . "); Console.ReadKey(true); } }
4
using System;
using System.Collections.Generic;using System.Text;namespace _ { public interface IBankAccount { void PayIn(decimal amount); bool Withdraw(decimal amount); decimal Balance { get; } } class SaverAccount : IBankAccount { private decimal balance; public decimal Balance { get { return balance; } } public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine("Withdraw failed."); return false; } public override string ToString() { return String.Format("Venus 银行存款:Balance={0,6:C}", balance); } } class GoldAccount : IBankAccount { private decimal balance; public decimal Balance { get { return balance; } } public void PayIn(decimal amount) { balance += amount; } public bool Withdraw(decimal amount) { if (balance >= amount) { balance -= amount; return true; } Console.WriteLine("Withdraw failed."); return false; } public override string ToString() { return String.Format("Jupiter 银行存款:Balance={0,6:C}", balance); } }class Program { static void Main(string[] args) { IBankAccount venusAccount = new SaverAccount(); IBankAccount jupiterAccount = new GoldAccount(); venusAccount.PayIn(200); jupiterAccount.PayIn(500); Console.WriteLine(venusAccount.ToString()); jupiterAccount.PayIn(400); jupiterAccount.Withdraw(500); jupiterAccount.Withdraw(100); Console.WriteLine(jupiterAccount.ToString()); Console.Write("按任意键继续 . . . "); Console.ReadKey(true); } } }