C#的override关键字
来源:互联网 发布:javashop b2b2c源码 编辑:程序博客网 时间:2024/05/12 18:46
override (C# Reference)
The override modifier is required to extend or modify the abstract or virtual implementation of an inherited method, property, indexer, or event.
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; } // Area method is required to avoid // a compile-time error. public override int Area() { return side * side; } static void Main() { 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(); }}// Output: Area of the square = 144
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, oroverride.
For more information about how to use the override keyword, see Versioning with the Override and New Keywords (C# Programming Guide) and Knowing when to use Override and New Keywords.
This example defines a base class named Employee, and a derived class named SalesEmployee. The SalesEmployee class includes an extra property, salesbonus, and overrides the method CalculatePay in order to take it into account.
class TestOverride{ public class Employee { public string name; // Basepay is defined as protected, so that it may be // accessed only by this class and derrived classes. protected decimal basepay; // Constructor to set the name and basepay values. public Employee(string name, decimal basepay) { this.name = name; this.basepay = basepay; } // Declared virtual so it can be overridden. public virtual decimal CalculatePay() { return basepay; } } // Derive a new class from Employee. public class SalesEmployee : Employee { // New field that will affect the base pay. private decimal salesbonus; // The constructor calls the base-class version, and // initializes the salesbonus field. public SalesEmployee(string name, decimal basepay, decimal salesbonus) : base(name, basepay) { this.salesbonus = salesbonus; } // Override the CalculatePay method // to take bonus into account. public override decimal CalculatePay() { return basepay + salesbonus; } } static void Main() { // Create some new employees. SalesEmployee employee1 = new SalesEmployee("Alice", 1000, 500); Employee employee2 = new Employee("Bob", 1200); Console.WriteLine("Employee4 " + employee1.name + " earned: " + employee1.CalculatePay()); Console.WriteLine("Employee4 " + employee2.name + " earned: " + employee2.CalculatePay()); }}/* Output: Employee4 Alice earned: 1500 Employee4 Bob earned: 1200*/
- C#的override关键字
- C#的New关键字和Override关键字对比
- C# new关键字和override
- C#关键字之override详解
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- C#关键字—override 和 new 的区别
- 浅谈C#中new、override、virtual关键字的区别
- 浅析C#中new、override、virtual关键字的区别
- 有关C#关键字new、override、virtual的总结
- c#的new、abstract、virtual、override,sealed关键字区别
- 线程优先级测试
- javascript 倒计时,兼容性有些问题。
- ubuntu下配置VIM做PHP的IDE开发环境
- Linux PPP 框架分析
- 伦敦与北京奥运开幕式戏说
- C#的override关键字
- 9月21日iphone5将上市——苹果员工
- C#的New关键字和Override关键字对比
- ADB 常用命令
- C#调用WebService
- 浅谈输入法编程(可用于注入)
- Android SDK中tools详解
- ExtJs formPanel(一、基础)
- 博客园文章爬取代码