码迷,mamicode.com
首页 > Windows程序 > 详细

override (C# 参考)--MSDN

时间:2014-10-13 16:32:59      阅读:256      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   io   os   使用   ar   

要扩展或修改继承的方法、属性、索引器或事件的抽象实现或虚实现,必须使用 override 修饰符。

 

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

 

override 方法提供从基类继承的成员的新实现。 override 声明重写的方法称为重写基方法。 重写的基方法必须与 override 方法具有相同的签名。 有关继承的信息,请参见继承(C# 编程指南)

不能重写非虚方法或静态方法。 重写的基方法必须是 virtual、abstract 或 override 的。

override 声明不能更改 virtual 方法的可访问性。 override 方法和 virtual 方法必须具有相同的访问级别修饰符

您不能使用 new、static 或 virtual 修饰符来修改 override 方法。

重写属性声明必须指定与继承属性完全相同的访问修饰符、类型和名称,并且被重写的属性必须是 virtual、abstract 或 override 的。

有关如何使用 override 关键字的更多信息,请参见使用 Override 和 New 关键字进行版本控制(C# 编程指南)了解何时使用 Override 和 New 关键字

此示例定义了一个名为 Employee 的基类和一个名为 SalesEmployee 的派生类。 SalesEmployee 类包括一个额外的属性 salesbonus,并重写方法 CalculatePay 以便将该属性考虑在内。

 

 1 class TestOverride
 2 {
 3     public class Employee
 4     {
 5         public string name;
 6 
 7         // Basepay is defined as protected, so that it may be 
 8         // accessed only by this class and derrived classes.
 9         protected decimal basepay;
10 
11         // Constructor to set the name and basepay values.
12         public Employee(string name, decimal basepay)
13         {
14             this.name = name;
15             this.basepay = basepay;
16         }
17 
18         // Declared virtual so it can be overridden.
19         public virtual decimal CalculatePay()
20         {
21             return basepay;
22         }
23     }
24 
25     // Derive a new class from Employee.
26     public class SalesEmployee : Employee
27     {
28         // New field that will affect the base pay.
29         private decimal salesbonus;
30 
31         // The constructor calls the base-class version, and
32         // initializes the salesbonus field.
33         public SalesEmployee(string name, decimal basepay, 
34                   decimal salesbonus) : base(name, basepay)
35         {
36             this.salesbonus = salesbonus;
37         }
38 
39         // Override the CalculatePay method 
40         // to take bonus into account.
41         public override decimal CalculatePay()
42         {
43             return basepay + salesbonus;
44         }
45     }
46 
47     static void Main()
48     {
49         // Create some new employees.
50         SalesEmployee employee1 = new SalesEmployee("Alice", 
51                       1000, 500);
52         Employee employee2 = new Employee("Bob", 1200);
53 
54         Console.WriteLine("Employee4 " + employee1.name + 
55                   " earned: " + employee1.CalculatePay());
56         Console.WriteLine("Employee4 " + employee2.name + 
57                   " earned: " + employee2.CalculatePay());
58     }
59 }
60 /*
61     Output:
62     Employee4 Alice earned: 1500
63     Employee4 Bob earned: 1200
64 */

 

override (C# 参考)--MSDN

标签:des   style   blog   http   color   io   os   使用   ar   

原文地址:http://www.cnblogs.com/joinne-pro/p/4022315.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!