第9章、接口

来源:互联网 发布:联想t460p指纹识别软件 编辑:程序博客网 时间:2024/04/28 18:55

9.5 接口作为参数

    class Program    {        static void Main(string[] args)        {            Program p = new Program();            Man m = new Man();         //这里,想实现谁接口里的方法,就实例化谁,然后在下边就传谁            //Woman w = new Woman();            p.Get(m);                  //传递的是实现了接口的对象                     Console.ReadLine();        }        public void Get(IPerson ipn)   //接口做为参数传递        {            ipn.Say();        }    }    /// <summary>    /// 一个人类的接口    /// </summary>    public interface IPerson    {        void Say();    }    /// <summary>    /// 一个男人的类    /// </summary>    public class Man : IPerson    {        public void Say()        {            Console.WriteLine("我是一个男人");        }    }    /// <summary>    /// 一个女人的类    /// </summary>    public class Woman : IPerson    {        public void Say()        {            Console.WriteLine("我是一个女人");        }    }


9.6 接口作为返回值

    //接口做为参数返回    class Program    {        static void Main(string[] args)        {            string a = Console.ReadLine();            Program pr = new Program();            pr.Getsay(a);            Console.ReadLine();        }        public Person Getsay(string a)        {            Person p = null;            switch (a)            {                case "1":                    p = new Man();                    p.Say();                    break;                case "2":                    p = new Woman();                    p.Say();                    break;            }            return p;        }    }    public interface Person    {        void Say();    }    public class Man : Person    {        public void Say()        {            Console.WriteLine("我是男人");        }    }    public class Woman : Person    {        public void Say()        {            Console.WriteLine("我是女人");        }    } 

9.7 接口类型数组

    //接口类型数组    class Program    {        static void Main(string[] args)        {            Person[] p = { new Man(),new Woman()};            foreach (Person i in p)            {                i.Say();            }            Console.ReadLine();        }    }    public interface Person    {        void Say();    }    public class Man : Person    {        public void Say()        {            Console.WriteLine("我是男人");        }    }    public class Woman : Person    {        public void Say()        {            Console.WriteLine("我是女人");        }    } 

9.8 使用Visual Studio 2010 实现接口


9.9 通过显式接口实现解决命名冲突

     


    //显示接口    class Program    {        static void Main(string[] args)        {            Man m = new Man();            if (m is Person)                ((Person)m).Say();//必须使用转换来访问Say()成员        }    }    public interface Person    {        void Say();    }    public class Man : Person    {        void Person.Say()//显示实现的成员总是隐式私有的,这些成员的对象级别不可用。        {            Console.WriteLine("我是男人");        }    }

9.10 设计接口的层次结构

      

    public interface IDrawable    {        void Draw();    }    public interface IAdvancedDraw : IDrawable    {        void DrawBox();    }    public class BitMapImage : IAdvancedDraw//实现所有方法    {        public void DrawBox()        {            throw new NotImplementedException();        }        public void Draw()        {            throw new NotImplementedException();        }    }
      接口类型的多重继承:接口继承多个基接口

9.11 构建可枚举类型(IEnumerable 和 IEnumerator)

    //IEnumerable接口的GetEnumerator() 方法,IEnumerator接口的MoveNext()方法,Current属性    class Program    {        static void Main(string[] args)        {            Car[] carArray = { new Car("大众", "100000"),new Car("奔驰", "200000"),new Car("奥迪", "300000")};            IEnumerator result = carArray.GetEnumerator();//1、System.Collection空间都继承于IEnumerable接口,IEnumerable接口的GetEnumerator() 方法返回 IEnumerator接口            while (result.MoveNext())//2、IEnumerator接口的MoveNext方法            {                Car CarM = result.Current as Car;//3、IEnumerator接口的Current属性                Console.WriteLine(CarM.Carname);            }            Console.ReadKey();        }    }    public class Car    {        public Car(string carname, string jg)//有参构造方法        {            Carname = carname;            Jg = jg;        }        private string carname;//汽车名称        private string jg;//价格        public string Carname        {            get { return carname; }            set { carname = value; }        }        public string Jg        {            get { return jg; }            set { jg = value; }        }    }

9.11.1 用yield 关键字构建迭代方法

迭代器是这样一个成员方法,它指定了容器内部项被foreach处理时,该如何返回


9.11.2 构造命名迭代器


9.11.3 迭代方法的内部表现


9.12 构建可克隆对象 ICloneable

对象的拷贝分为:浅拷贝和深拷贝。ICloneable仅有一个Clone方法使我们无法从命名的角度去区分到底是哪个拷贝。

浅拷贝:将对象的字段复制到副本(新的对象)中,同时将字段的值也赋值过去,但是引用类型字段只复制引用,而不是引用类型本身。这意味着,源对象引用类型字段的值改变了,会影响到副本中对应的值也改变;

深拷贝:将对象的字段复制到副本(新的对象)中,无论是值类型还是引用类型字段,都会复制类型本身及类型的值。这意味着,源对象引用类型字段的值改变了,不会影响到副本中对应的值;


9.13 构建可比较对象 IComparable


原创粉丝点击