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.

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(369)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示