是否在C#.net中隐式使用了“new”关键字?

时间:2014-03-25 16:01:56

标签: c# .net oop inheritance override

具有以下类,覆盖(或不覆盖)Draw方法

using System;

namespace ConsoleApplication1
{
    public class Shape {
        public virtual void Draw() {
            Console.WriteLine("base     Drawing a shape");
        }
    }

    class Rectangle : Shape {
        public new virtual void Draw() {
            Console.WriteLine("new virt Drawing a rectangle");
            base.Draw();
        }
    }
    class Square : Rectangle {
        public override void Draw() {
            Console.WriteLine("new      Drawing a square");
            base.Draw();
        }
    }

    class Triangle : Shape {
        public override void Draw() {
            Console.WriteLine("override Drawing a triangle");
            base.Draw();
        }
    }
    class SuperTriangle : Triangle {
        public new void Draw() {
            Console.WriteLine("new Drawing a SuperTriangle");
            base.Draw();
        }
    }
    class DefaultTriangle : Triangle {
        public void Draw() {
            Console.WriteLine("(-)      Drawing a DefaultTriangle");
            base.Draw();
        }
    }

    class Program {
        static void Main(string[] args) {
            Console.WriteLine(@"
Rectangle       : Shape (new virtual)
Square          : Rectangle (override)

Triangle        : Shape (override)
SuperTriangle   : Triangle (new)
DefaultTriangle : Triangle (-)

");
            System.Collections.Generic.List<Shape> shapes = 
                new System.Collections.Generic.List<Shape>() {
                new Rectangle(), new Square(), 
                new Triangle(), new SuperTriangle(), new DefaultTriangle()};

            foreach (Shape s in shapes) {
                Console.WriteLine();
                Console.WriteLine("(Shape) " + s.GetType().Name);
                Console.WriteLine("________________________");
                s.Draw();
                Console.WriteLine();
            }
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
}

我有以下输出:

Rectangle       : Shape (new virtual)
Square          : Rectangle (override)

Triangle        : Shape (override)
SuperTriangle   : Triangle (new)
DefaultTriangle : Triangle (-)



(Shape) Rectangle
________________________
base     Drawing a shape


(Shape) Square
________________________
base     Drawing a shape


(Shape) Triangle
________________________
override Drawing a triangle
base     Drawing a shape


(Shape) SuperTriangle
________________________
override Drawing a triangle  // <<<< !!!
base     Drawing a shape


(Shape) DefaultTriangle
________________________
override Drawing a triangle  // <<<< !!!
base     Drawing a shape 

这是否意味着new keyword不是强制性的,而是隐式应用,如果没有指定其他关键字?

PS。警告:

  

警告1'ConsoleApplication1.DefaultTriangle.Draw()'隐藏继承   成员'ConsoleApplication1.Triangle.Draw()'。使目前   成员覆盖该实现,添加覆盖关键字。   否则,请添加关键字。

2 个答案:

答案 0 :(得分:2)

您拥有的代码会生成一个警告,专门回答您的问题。

它告诉您没有指出是应该覆盖该方法还是隐藏基类实现,除非另有说明,否则它会隐藏该实现。

答案 1 :(得分:0)

警告明确告诉实际行为:

  

'方法'隐藏继承的成员'基本方法'

所以是的,new行为是默认行为。如果您想了解其背后的推理,或了解有关整个问题的更多详细信息,请参阅此@EricLippertthread的完美答案。