override (C# Reference)
2016-03-09 17:12
351 查看
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:
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 virtual, abstract, 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 new, static, 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 virtual, abstract, 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.
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 virtual, abstract, 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 new, static, 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 virtual, abstract, 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.
相关文章推荐
- 详解AngularJS控制器的使用
- js 中使用工厂方法和构造器方法
- js页面滚动到底部时自动加载下一页数据
- javascript中的数组
- IntelliJ-Idea工具的常用快捷键和在html里的常用快捷键
- JavaScript学习 - 数组
- Manthan, Codefest 16(A)暴力
- 移动html特殊链接:打电话,发短信,发邮件
- rails torial 中css/js引用的版本坑
- [RxJS] Displaying Initial Data with StartWith
- 4 Javascript:决策-if/else语句
- virtual (C# Reference)
- jQuery中 end(); 的用法
- [css] 清除浮动的方式总结
- js设计模式--鸭子类型
- Bootstrap—后台框架Ace
- [RxJS] Updating Data with Scan
- jquery.validate.js常用扩展函数
- jQuery 根据JSON数据动态生成表格
- WebPack系列教程(九):故障排除