继承(对象生命周期) + 覆盖[new](索引函数) + 重载[virtual/override]

继承(对象生命周期)

* 如下程序, 注意基类和派生类 构造方法 和 析构函数的 调用顺序

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{
    class lifecycle
    {
        public static void Main()
        {
            son sn = new son();
            System.GC.Collect();  //垃圾收集, 调用析构函数
        }
    }

    public class grandsire
    {
        public grandsire()
        {
            Console.WriteLine("调用grandsire构造函数");
        }

        ~grandsire()
        {
            Console.WriteLine("调用grandsire析构函数");
        }
    }

    public class father : grandsire
    {
        public father()
        {
            Console.WriteLine("调用father构造函数");
        }

        ~father()
        {
            Console.WriteLine("调用father析构函数");
        }
    }

    public class son : father
    {
        public son() //隐式调用基类构造函数 相当于public son():base(),但若基类构造函数带参数则派生类不能自动继承,必须显式调用
        {   //eg: 基类构造函数:public contact(string name){...}, 则派生时public Family(string name):base(name){...}
            Console.WriteLine("调用son构造函数");
            System.GC.Collect();
        }

        ~son()
        {
            Console.WriteLine("调用son析构函数");
        }
    }
}

 

 

结果:

调用grandsire构造函数
调用father构造函数
调用son构造函数
调用son析构函数
调用father析构函数
调用grandsire析构函数

 

//------------------------------------------------------------------------------------------

覆盖[new]

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{
    class InheritSample
    {
        public static void Main()
        {
            //继承
            business c1 = new business();
            c1.Name = "赵丽";
            c1.title = "经理";
            c1["办公电话"] = "010-1234567";
            c1["传真"] = "888888";
            c1.Output();
            Console.WriteLine(" ");

            Family c2 = new Family();
            c2.Name = "王强";
            c2.Relation = "表弟";
            c2.Output();

            Console.Read();
        }
    }

    public class Contact
    {
        //protected 确保可以被派生类访问
        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 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 void Output()
        {
            Console.WriteLine("姓名:{0}", m_name);
            Console.WriteLine("住宅电话:{0}", m_homePhone);
            Console.WriteLine("办公电话:{0}", m_busiPhone);
            Console.WriteLine("手机:{0}", m_mobilePhone);
        }
    }

    public class business : Contact
    {
        protected string m_busiFax = "未知";
        protected string m_title = "女士/先生";

        public string title
        {
            get { return m_title; }
            set { m_title = value; }
        }

        ////1. new 覆盖基类方法
        public new void Output()
        {
            Console.WriteLine("{0} {1}", m_name, m_title);
            Console.WriteLine("办公电话:{0} 手机:{1}", m_busiPhone, m_mobilePhone);
            Console.WriteLine("传真:{0}", m_busiFax);
        }

        //2. new 覆盖基类的索引函数
        public new string this[string phoneType]
        {
            //get
            //{
            //    switch (phoneType)
            //    {
            //        case "住宅电话":
            //            return m_homePhone;
            //        case "办公电话":
            //            return m_busiPhone;
            //        case "传真":
            //            return m_busiFax;
            //        case "手机":
            //            return m_mobilePhone;
            //        default:
            //            return null;
            //    }
            //}
            //set
            //{
            //    switch (phoneType)
            //    {
            //        case "住宅电话":
            //            m_homePhone = value;
            //            break;
            //        case "办公电话":
            //            m_busiPhone = value;
            //            break;
            //        case "传真":
            //            m_busiFax = value;
            //            break;
            //        case "手机":
            //            m_mobilePhone = value;
            //            break;
            //        default:
            //            break;
            //    }
            //}

            //使用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 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; }
        }


        ////1. new 覆盖基类方法
        public new void Output()
        {
            Console.WriteLine("{0} {1}", m_name, m_relation);
            Console.WriteLine("住宅电话:{0}", m_homePhone);
            Console.WriteLine("办公电话:{0} 手机:{1}", m_busiPhone, m_mobilePhone);
        }
    }
}

 

结果:

赵丽 经理
办公电话:010-1234567 手机:未知
传真:888888

王强 表弟
住宅电话:未知
办公电话:未知 手机:未知

//------------------------------------------------------------------------------------------

重载[virtual/override]

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{
    class virtualInheritSample
    {
        public static void Main()
        {
            DerivedClass c = new DerivedClass();
            c.Call();
            Console.Read();
        }
    }

    public class BaseClass
    {
        public void A()
        {
            Console.WriteLine("调用基类方法A");
        }

        public virtual void B()
        {
            Console.WriteLine("调用基类方法B");
        }
    }

    public class DerivedClass : BaseClass
    {
        public new void A()
        {
            Console.WriteLine("调用派生类方法A");
        }

        public override void B()
        {
            Console.WriteLine("调用派生类方法B");
        }

        public void Call()
        {
            this.A();
            this.B();
            ((BaseClass)this).A();  //强制转为基类对象
            ((BaseClass)this).B();  //调用基类虚函数 即调用其派生
        }
    }

}

 结果:

调用派生类方法A
调用派生类方法B
调用基类方法A
调用派生类方法B

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApp
{
    class ATM_overrideSample
    {
        public static void Main()
        {
            BankCard[] cards = new BankCard[3];
            cards[0] = new BankCard();
            cards[1] = new ForeignBankCard();
            cards[2] = new LimitBankCard();
            foreach (BankCard card in cards)
            {
                card.Deposit(5000);
                card.Withdraw(2000);
            }

            Console.Read();
        }
    }

    public class BankCard

    {
        protected decimal m_amount;
        public virtual void Deposit(decimal x)
        {
            m_amount += x;
            Console.WriteLine("--------------------------------");
            Console.WriteLine("存款{0}元,余额{1}元", x, m_amount);
        }

        public virtual void Withdraw(decimal x)
        { 
            if(m_amount>x)
            { 
                m_amount -= x;
                Console.WriteLine("取款{0}元,余额{1}元",x,m_amount);
            }
            else
            {
                Console.WriteLine("余额不足");
            }
        }
    }

    public class ForeignBankCard : BankCard
    {
        public override void Withdraw(decimal x)
        {
            decimal fee = x * 0.01M;
            if (m_amount > x + fee)
            {
                m_amount -= (x + fee);
                Console.WriteLine("取款{0}元,手续费{1}元,余额{2}元", x, fee, m_amount);
            }
            else
            {
                Console.WriteLine("余额不足");
            }
        }
    }

    public class LimitBankCard : BankCard
    {
        public override void Withdraw(decimal x)
        {
            if (x >= 1000)
            {
                Console.WriteLine("对不起,此卡限制1000元以上取款");
            }
            else
            {
                base.Withdraw(x);
            }
        }
    }
}

 结果:

--------------------------------
存款5000元,余额5000元
取款2000元,余额3000元
--------------------------------
存款5000元,余额5000元
取款2000元,手续费20.00元,余额2980.00元
--------------------------------
存款5000元,余额5000元
对不起,此卡限制1000元以上取款

posted @ 2012-11-28 17:35  streetpasser  阅读(234)  评论(0编辑  收藏  举报