类的继承

一。隐藏基类成员与访问基类成员

复制代码
using System;

namespace MaskMember
{
    // 基类
    class SomeClass
    {
        public string Field1 = "Field1 -- In the base class";

        public void Method1(string value)
        {
            Console.WriteLine($"SomeClass.Method1: {value}");
        }
    }


    // 派生类
    class OtherClass:SomeClass
    {
        // 隐藏基类字段
        new public string Field1 = "Field1 -- In the derived class";

        // 隐藏基类方法
        new public void Method1(string value)
        {
            Console.WriteLine($"OtherClass.Method1: {value}");
        }

        // 派生类中使用基类成员
        public void Method2()
        {
            base.Method1(base.Field1);
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            // 使用屏蔽成员
            OtherClass oc = new OtherClass();
            oc.Method1(oc.Field1);
            oc.Method2();

            Console.WriteLine("---------------------");

            // 使用基类引用
            SomeClass sc = (SomeClass)oc;
            sc.Method1(sc.Field1);
            //sc.Method2();     // 错误:基类引用无法访问派生类成员
        }
    }
}
复制代码

二。虚方使基类引用访问“升至”派生类中

复制代码
using System;

namespace VirtualMethod
{
    // 基类
    class MyBaseClass
    {
        virtual public void Print()
        {
            Console.WriteLine("This is the base class.");
        }
    }


    class MyDerivedClass:MyBaseClass
    {
        public override void Print()
        {
            Console.WriteLine("This is the derived class.");
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass derived = new MyDerivedClass();
            MyBaseClass mybc = (MyBaseClass)derived;

            derived.Print();
            mybc.Print();   // 基类虚方法引用上升到派生类重写方法
        }
    }
}
复制代码

三。多重派生“升至”最高派生版本,覆盖方法不会上升到派生类

复制代码
using System;

namespace MultiVirtualMethod
{
    class Program
    {
        // 基类
        class MyBaseClass
        {
            virtual public void Print()
            {
                Console.WriteLine("This is the base class.");
            }

            public void Print1()
            {
                Console.WriteLine("This is the base class -- coverage method.");
            }
        }


        // 派生类
        class MyDerivedClass:MyBaseClass
        {
            public override void Print()
            {
                Console.WriteLine("This is the derived class.");
            }
        }


        // 最高派生类
        class SencondDerived : MyDerivedClass
        {
            public override void Print()
            {
                Console.WriteLine("This is the second derived class.");
            }

            new public void Print1()
            {
                Console.WriteLine("This is the seconde derived class, coverage method.");
            }
        }


        static void Main(string[] args)
        {
            SencondDerived derived = new SencondDerived();
            MyBaseClass mybc = (MyBaseClass)derived;

            derived.Print();
            mybc.Print();       // 基类引用被上升至最高派生类

            derived.Print1();
            mybc.Print1();      // 屏蔽方法--基类引用不会被上升至派生类
        }
    }
}
复制代码

四。重写其它成员类型

复制代码
using System;

namespace OverrideOhterMembers
{
    // 基类
    class MyBaseClass
    {
        private int _myInt = 5;
        virtual public int MyProperty
        {
            get { return _myInt; }
        }
    }


    // 派生类
    class MyDerivedClass:MyBaseClass
    {
        private int _myInt = 10;
        public override int MyProperty
        {
            get { return _myInt; }
        }
    }


    class Program
    {
        static void Main(string[] args)
        {
            MyDerivedClass derived = new MyDerivedClass();
            MyBaseClass mybc = (MyBaseClass)derived;

            Console.WriteLine(derived.MyProperty);
            Console.WriteLine(mybc.MyProperty);     // 上升到派生类
        }
    }
}
复制代码

 

posted @   獨懼  阅读(12)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示