关于接口内容的浅解

来源:互联网 发布:网络作战司令部 编辑:程序博客网 时间:2024/06/02 02:59
  1. 接口名传统上以字母I开头,以便知道这是一个接口
  2. 从接口中派生完全独立于从类中派生
  3. 在声明接口成员的时候,只要指明接口成员的名称和参数就可以了,接口一旦被继承,子类需要提供方法的所有实现代码。
  4. 接口声明不包括数据成员,只能包含方法、属性、事件、索引等成员。不允许声明成员上的修饰符,即使是pubilc都不行,因为接口成员总是公有的,也不能声明为虚拟和静态的。如果需要修饰符,最好让实现类来声明。
  5. 接口和类都可以继承多个接口
    复制代码
    using System;namespace ConsoleApplication3{    class Program    {        static void Main(string[] args)        {            /*              在这里我们把它们声明为IBankAccount引用的方式,而没有声明为类的引用。             这表示它们可以指向实现这个接口的任何类的实例。             但是缺点是:             只能通过这些引用调用接口中的方法,如果要调用由类执行的、不在接口中的方法(比如这里重写的ToString()方法),             就需要把引用强制转换为适合的类型。             但是这里没有任何显示的转换,因为ToString()是一个System.Object方法,C#编译器知道任何类都支持这个方法             换言之,从接口道System.Object的数据类型转换时隐式的)             */            IBankAccount abcAcount = new SaverAccount();            abcAcount.PayIn(200);            abcAcount.Withdraw(100);            Console.WriteLine(abcAcount.ToString());            IBankAccount icbcAcount = new GoldAccount();            icbcAcount.PayIn(500);            icbcAcount.Withdraw(600);            icbcAcount.Withdraw(100);            Console.WriteLine(icbcAcount.ToString());            Console.ReadLine();        }    }    // 声明接口在语法上和声明抽象类完全相同    public interface IBankAccount    {        // 接口成员的访问级别是默认的(默认为public),所以在声明时不能再为接口成员指定任何访问修饰符,        // 否则 编译器会报错。        void PayIn(decimal amount);        bool Withdraw(decimal amount);        decimal Balance        {            get;        }    }        public class SaverAccount : IBankAccount    {        private decimal balance;        public void PayIn(decimal amount)        {            balance += amount;        }        public bool Withdraw(decimal amount)        {            if (balance >= amount)            {                balance -= amount;                return true;            }            Console.WriteLine("Withdraw attempt failed");            return false;        }        public decimal Balance        {            get            {                return balance;            }        }        public override string ToString()        {            return String.Format(" ABC Bank Saver: Balance = {0,6:C}", balance);        }    }    public class GoldAccount : IBankAccount    {        private decimal balance;        public void PayIn(decimal amount)        {            balance += amount;        }        public bool Withdraw(decimal amount)        {            if (balance >= amount)            {                balance -= amount;                return true;            }            Console.WriteLine("Withdraw attempt failed");            return false;        }        public decimal Balance        {            get            {                return balance;            }        }        public override string ToString()        {            return String.Format("ICBC Bank2 Saver: Balance = {0,6:C}", balance);        }    }}
    复制代码

     

  6. 接口的强大之处在于,它可以引用任何实现给接口的类,例如,可以构造接口数组,其中的每个元素都是不同的类:

    1             IBankAccount[] accounts = new IBankAccount[2];2             accounts[0] = new SaverAccount();3             accounts[1] = new GoldAccount();
原创粉丝点击