代码之家  ›  专栏  ›  技术社区  ›  Noor A Shuvo

继承如何在C*[复制]中起作用

  •  -2
  • Noor A Shuvo  · 技术社区  · 5 年前

    这个问题已经有了答案:

    我有密码

    using System;
    
    namespace ExploreOOP
    {
        class Program
        {
            static void Main(string[] args)
            {    
                Base1 b1 = new Derived1();
                Derived1 d1 = new Derived1();
                b1.write();
                d1.write();
    
                Console.WriteLine();
                Console.WriteLine();
    
    
                Base2 b2 = new Derived2();
                Derived2 d2 = new Derived2();
                b2.write();
                d2.write();
    
    
                Console.WriteLine();
                Console.WriteLine();
    
                Base3 b3 = new Derived3();
                Derived3 d3 = new Derived3();
                b3.write();
                d3.write();
    
                Console.ReadKey();
    
            }
        }
    
        public class Base1
        {
            public void write()
            {
                Console.WriteLine("Inside Base1 class");
            }
        }
    
        public class Derived1 : Base1
        {
            public void write()
            {
                Console.WriteLine("Inside Derived1 class");
            }
        }
    
        public class Base2
        {
            public virtual void write()
            {
                Console.WriteLine("Inside Base2 class");
            }
        }
    
        public class Derived2 : Base2
        {
            public override void write()
            {
                Console.WriteLine("Inside Derived2 class");
            }
        }
    
        public class Base3
        {
            public void write()
            {
                Console.WriteLine("Inside Base3 class");
            }
        }
    
        public class Derived3 : Base3
        {
            new public void write()
            {
                Console.WriteLine("Inside Derived3 class");
            }
        }
    
    }
    

    它输出,

    Inside Base1 class
    Inside Derived1 class
    
    
    Inside Derived2 class
    Inside Derived2 class
    
    
    Inside Base3 class
    Inside Derived3 class
    

    所以我的问题是,

    public class Base1
    {
        public void write()
        {
            Console.WriteLine("Inside Base1 class");
        }
    }
    
    public class Derived1 : Base1
    {
        public void write()
        {
            Console.WriteLine("Inside Derived1 class");
        }
    }
    

    public class Base3
    {
        public void write()
        {
            Console.WriteLine("Inside Base3 class");
        }
    }
    
    public class Derived3 : Base3
    {
        new public void write()
        {
            Console.WriteLine("Inside Derived3 class");
        }
    }
    

    做同样的事吗?

    2 回复  |  直到 5 年前
        1
  •  2
  •   Mor A.    5 年前

    new modifier 以下内容:

    当用作声明修饰符时,新关键字显式隐藏从基类继承的成员。隐藏继承的成员时,该成员的派生版本将替换基类版本。虽然您可以隐藏成员而不使用新的修饰符,但是您得到编译器警告。如果使用新来显式隐藏成员,则会抑制此警告。

    这意味着是的, Derived3 Derived1 基本上是一样的,除了第一种情况会输出 警告 ,特别是

    warning CS0108: 'Derived1.write()' hides inherited member 'Base1.write()'. Use the new keyword if hiding was intended.