• 接口 interface


    1. 接口和抽象类有很多共同点, 都是声明变量, 不能被实例化. 并且其中的方法都不提供实现代码.
    2. 接口的任何派生类型(类或接口)都必须继承该接口定义的所有方法, 并且其派生类如果是非抽象的, 则必须实现接口的所有方法.
       抽象类的派生类必须继承其所有抽象方法, 并且其派生类如果是非抽象的, 则必须实现抽象类的所有抽象方法.

    3. 接口抽象化更高, 接口的方法全是抽象方法, 而抽象类中的方法可以有抽象方法也可以有非抽象方法. 另外, 接口还不能有字段成员.

    4. 如果派生类既继承了类又继承了接口, 则定义时先写基类再写接口. 如: class creditcard : bankcard,Iloan
       接口定义时 方法上不用访问限制修饰符.
       多接口定义了相同方法, 而派生类又继承了这些接口, 则用一个方法即可实现. 如果要区分, 可以用 "接口名.方法名" 写多个相同方法.

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApp
    {
        class InheritSample
        {
            public static void Main()
            {
                Business c1 = new Business("赵丽");
                c1.title = "经理";
                c1["办公电话"] = "010-1234567";
                Family c2 = new Family("王强");
                c2.Relation = "表弟";
                c2.Birthday = new DateTime(1980, 12, 1);
                c2["手机"] = "13801284321";
                
                //因为Bussiness通过重载Output方法实现多态,故强两次调用output方法的输出是相同的
                c1.Output();
                Console.WriteLine("----------------------------");
                ((Contact)c1).Output();
                Console.WriteLine("----------------------------");
                ((IOutput)c1).Output();
                Console.WriteLine("----------------------------");
                c2.Output();
                Console.WriteLine("----------------------------");
                ((Contact)c2).Output();
    
                Console.Read();
            }
        }
    
        //接口:可输出IOutput
        public interface IOutput
        {
            void Output();
        }
    
        public class Contact : IOutput, IComparable
        {
            protected string m_name;
            protected string m_homePhone = "未知";
            protected string m_busiPhone = "未知";
            protected string m_mobilePhone = "未知";
    
            public string Name
            {
                get { return m_name; }
                set { m_name = value; }
            }
    
            public virtual string this[string phoneType]  //带参数分支的属性  索引函数
            {
                get
                {
                    switch (phoneType)
                    {
                        case "住宅电话":
                            return m_homePhone;
                        case "办公电话":
                            return m_busiPhone;
                        case "手机":
                            return m_mobilePhone;
                        default:
                            return null;
                    }
                }
                set
                {
                    switch (phoneType)
                    {
                        case "住宅电话":
                            m_homePhone = value;
                            break;
                        case "办公电话":
                            m_busiPhone = value;
                            break;
                        case "手机":
                            m_mobilePhone = value;
                            break;
                        default:
                            break;
                    }
                }
            }
    
            public Contact(string name)
            {
                m_name = name;
            }
    
            public virtual void Output()
            {
                Console.WriteLine("姓名:{0}", m_name);
                Console.WriteLine("住宅电话:{0}", m_homePhone);
                Console.WriteLine("办公电话:{0}", m_busiPhone);
                Console.WriteLine("手机:{0}", m_mobilePhone);
            }
    
            public int CompareTo(object obj)
            {
                if (obj is Contact)
                {
                    return m_name.CompareTo(((Contact)obj).m_name);
                }
                return -1;
            }
        }
    
        //派生类:商务Business
        public class Business : Contact, IOutput
        {
            protected string m_busiFax = "未知";
            protected string m_title = "女士/先生";
    
            public string title
            {
                get { return m_title; }
                set { m_title = value; }
            }
    
            public override string this[string phoneType]
            {
                //使用base简化为↓
                get
                {
                    switch (phoneType)
                    {
                        case "传真":
                            return m_busiFax;
                        default:
                            return base[phoneType];
                    }
                }
                set
                {
                    switch (phoneType)
                    {
                        case "传真":
                            m_busiFax = value;
                            break;
                        default:
                            base[phoneType] = value;
                            break;
                    }
                }
            }
    
            public Business(string name) : base(name) { }
    
            //重载基类方法
            public override void Output()
            {
                base.Output();
                Console.WriteLine("传真:{0}", m_busiFax);
            }
    
            //实现接口方法
            void IOutput.Output()
            {
                Console.WriteLine(m_name + m_title);
                Console.WriteLine("(B){0} (F){1} (H){2} (M){3} ", m_busiPhone, m_busiFax, m_homePhone, m_mobilePhone);
            }
        }
    
        public class Family : Contact
        {
            protected string m_relation;
            protected DateTime m_birthday;
    
            public string Relation
            {
                get { return m_relation; }
                set { m_relation = value; }
            }
    
            public DateTime Birthday
            {
                get { return m_birthday; }
                set { m_birthday = value; }
            }
    
            public Family(string name) : base(name) { }
    
            //覆盖
            public new void Output()
            {
                Console.WriteLine(m_relation + m_name);
                Console.WriteLine("生日:{0}", m_birthday);
                Console.WriteLine("(B){0} (H){1} (M){2} ", m_busiPhone, m_homePhone, m_mobilePhone);
            }
        }
    }
    

       姓名:赵丽
    住宅电话:未知
    办公电话:010-1234567
    手机:未知
    传真:未知
    ----------------------------
    姓名:赵丽
    住宅电话:未知
    办公电话:010-1234567
    手机:未知
    传真:未知
    ----------------------------
    赵丽经理
    (B)010-1234567 (F)未知 (H)未知 (M)未知
    ----------------------------
    表弟王强
    生日:1980-12-1 0:00:00
    (B)未知 (H)未知 (M)13801284321
    ----------------------------
    姓名:王强
    住宅电话:未知
    办公电话:未知
    手机:13801284321

    http://www.cnblogs.com/streetpasser/archive/2012/11/30/2796337.html  <ConsoleApp2引入ConsoleApp>

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    using ConsoleApp;  //引入
    
    namespace ConsoleApp
    {
        class InheritSample2
        {
            public static void Main()
            {
                Business c1 = new Business("赵丽");
                c1.title = "经理";
                c1["办公电话"] = "010-1234567";
    
                Family c2 = new Family("王强");
                c2.Relation = "表弟";
                c2.Birthday = new DateTime(1980, 12, 1);
                c2["手机"] = "13801284321";
    
                //调用
                ContactAssemble asm = new ContactAssemble(10);
                asm.SetAt(0,c1);
                asm.SetAt(1, c2);
                asm.Output();
                Console.WriteLine();
                ((IOutput )asm).Output();
    
                Console.Read();
            }
        }
    
        //集合接口 -- 可使用不同的数据结构来实现
        public interface IAssemble
        {
            int length { get;}                   //取得集合长度
            object GetAt(int index);             //读取集合元素
            void SetAt(int index, object obj);   //设置集合元素
        }
    
        //抽象类: 集合CzAssemble
        public abstract class CzAssemble : IAssemble
        {
            protected object[] m_list;
    
            public int length
            {
                get { return m_list.Length; }
            }
    
            public CzAssemble(int length)
            {
                m_list = new object[length];
            }
    
            public virtual object GetAt(int index)
            {
                return m_list[index];
            }
    
            public virtual void SetAt(int index, object obj)
            {
                m_list[index] = obj;
            }
        }
    
        //派生类:联系人集合
        public class ContactAssemble : CzAssemble, IOutput
        {
            public ContactAssemble(int length) : base(length) { }
    
            public new Contact GetAt(int index)
            {
                return (Contact)m_list[index];
            }
    
            public void SetAt(int index, Contact c)
            {
                m_list[index] = c;
            }
    
            public void Output()
            {
                foreach (Contact c in m_list)
                {
                    if (c != null)
                    {
                        c.Output();
                        Console.WriteLine("-------------------------");
                    }
                }
            }
    
            void IOutput.Output()
            {
                foreach (Contact c in m_list)
                {
                    if (c != null)
                    {
                        ((IOutput )c).Output();
                        Console.WriteLine("-------------------------");
                    }
                }
            }
    
        }
    
    }
    

     姓名:赵丽
    住宅电话:未知
    办公电话:010-1234567
    手机:未知
    传真:未知
    -------------------------
    姓名:王强
    住宅电话:未知
    办公电话:未知
    手机:13801284321
    -------------------------

    赵丽经理
    (B)010-1234567 (F)未知 (H)未知 (M)未知
    -------------------------
    姓名:王强
    住宅电话:未知
    办公电话:未知
    手机:13801284321
    -------------------------

    使用ArrayList

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections ;
    using ConsoleApp;  //引入
    
    namespace ConsoleAppl3
    {
        class InheritSample3
        {
            static void Main(string[] args)
            {
                Business c1 = new Business("赵丽");
                c1.title = "经理";
                c1["办公电话"] = "010-1234567";
    
                Family c2 = new Family("王强");
                c2.Relation = "表弟";
                c2.Birthday = new DateTime(1980, 12, 1);
                c2["手机"] = "13801284321";
    
                ContactAssemble asm = new ContactAssemble(10);
                asm.Add(c1);
                asm.Add(c2);
                asm.Output();
                Console.WriteLine();
                ((IOutput)asm).Output();
    
                Console.Read();
            }
        }
    
        public class ContactAssemble : ArrayList, IOutput
        {
            public ContactAssemble(int length) : base(length) { }
            public void Output()
            {
                foreach (Contact c in this)
                {
                    if (c != null)
                    {
                        c.Output();
                        Console.WriteLine("-----------------------------------");
                    }
                }
            }
    
            void IOutput.Output()
            {
                foreach (Contact c in this)
                {
                    if (c != null)
                    {
                        ((IOutput )c).Output();
                        Console.WriteLine("-----------------------------------");
                    }
                }
            }
        }
    
        
    }
    
  • 相关阅读:
    LLC半桥谐振变换器调试记录
    工业派学习记录
    ubuntu 命令记录
    电容单位换算
    Windows快捷键
    CAN总线学习笔记
    Scala 基础(十五):Scala 模式匹配(三)
    Scala 基础(十四):Scala 模式匹配(二)
    Scala 基础(十三):Scala 模式匹配(一)
    scala 数据结构(十一):流 Stream、视图 View、线程安全的集合、并行集合
  • 原文地址:https://www.cnblogs.com/streetpasser/p/2794744.html
Copyright © 2020-2023  润新知