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

C# virtual 这个关键字

时间:2017-07-03 20:57:07      阅读:286      评论:0      收藏:0      [点我收藏+]

标签:rac   with   following   eve   des   can   imp   bsp   name   

The virtual keyword is used to modify a method, property, indexer, or event declaration and allow for it to be overridden in a derived class. For example, this method can be overridden by any class that inherits it:

 

public virtual double Area()   
{  
    return x * y;  
}

 You cannot use the virtual modifier with the staticabstract, private, or override modifiers. The following example shows a virtual property:

class MyBaseClass
{
    // virtual auto-implemented property. Overrides can only
    // provide specialized behavior if they implement get and set accessors.
    public virtual string Name { get; set; }

    // ordinary virtual property with backing field
    private int num;
    public virtual int Number
    {
        get { return num; }
        set { num = value; }
    }
}


class MyDerivedClass : MyBaseClass
{
    private string name;

   // Override auto-implemented property with ordinary property
   // to provide specialized accessor behavior.
    public override string Name
    {
        get
        {
            return name;
        }
        set
        {
            if (value != String.Empty)
            {
                name = value;
            }
            else
            {
                name = "Unknown";
            }
        }
    }

}

 

C# virtual 这个关键字

标签:rac   with   following   eve   des   can   imp   bsp   name   

原文地址:http://www.cnblogs.com/systembighunter/p/7112797.html

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