override (C# Reference)

https://msdn.microsoft.com/en-us/library/ebca9ah3.aspx

The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.

 

Example

In this example, the Square class must provide an overridden implementation of Area because Area is inherited from the abstract ShapesClass:

abstract class ShapesClass
    {
        abstract public int Area();
    }

    class Square : ShapesClass
    {
        int side = 0;

        public Square(int n)
        {
            side = n;
        }

        /// <summary>
        ///  Area method is required to avoid a compile-time error.
        /// </summary>
        /// <returns></returns>
        public override int Area()
        {
            return side*side;
        }

        internal static void Method()
        {
            Square sq = new Square(12);
            Console.WriteLine("Area of the square = {0}", sq.Area());
        }

        interface I
        {
            void M();
        }

        abstract class C : I
        {
            public abstract void M();
        }

    }

 

 

An override method provides a new implementation of a member that is inherited from a base class.

The method that is overridden by an override declaration is known as the overridden base method.

The overridden base method must have the same signature as the override method.

For information about inheritance, see Inheritance (C# Programming Guide).

 

 

 

You cannot override a non-virtual or static method.

The overridden base method must be virtualabstract, or override.

An override declaration cannot change the accessibility of the virtual method.Both the override method and the virtual method must have the same access level modifier.

You cannot use the newstatic, or virtual modifiers to modify an override method.

An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property, and the overridden property must be virtualabstract, or override.

For more information about how to use the override keyword,

see Versioning with the Override and New Keywords (C# Programming Guide) 

andKnowing when to use Override and New Keywords.

 

posted @ 2016-03-09 17:12  ChuckLu  阅读(368)  评论(0编辑  收藏  举报