C#里面virtual 和 overrider的区别
最好有源代码实例

解决方案 »

  1.   

    MSDN里有说明的:A virtual property declaration specifies that the accessors of the property are virtual. The virtual modifier applies to both accessors of a read-write property — it is not possible for only one accessor of a read-write property to be virtual.An abstract property declaration specifies that the accessors of the property are virtual, but does not provide an actual implementation of the accessors. Instead, non-abstract derived classes are required to provide their own implementation for the accessors by overriding the property. Because an accessor for an abstract property declaration provides no actual implementation, its accessor-body simply consists of a semicolon.A property declaration that includes both the abstract and override modifiers specifies that the property is abstract and overrides a base property. The accessors of such a property are also abstract.Abstract property declarations are only permitted in abstract classes (Section 10.1.1.1).The accessors of an inherited virtual property can be overridden in a derived class by including a property declaration that specifies an override directive. This is known as an overriding property declaration. An overriding property declaration does not declare a new property. Instead, it simply specializes the implementations of the accessors of an existing virtual property.An overriding property declaration must specify the exact same accessibility modifiers, type, and name as the inherited property. If the inherited property has only a single accessor (i.e., if the inherited property is read-only or write-only), the overriding property must include only that accessor. If the inherited property includes both accessors (i.e., if the inherited property is read-write), the overriding property can include either a single accessor or both accessors.An overriding property declaration may include the sealed modifier. Use of this modifier prevents a derived class from further overriding the property. The accessors of a sealed property are also sealed.Except for differences in declaration and invocation syntax, virtual, sealed, override, and abstract accessors behave exactly like virtual, sealed, override and abstract methods. Specifically, the rules described in Section 10.5.3, Section 10.5.4, Section 10.5.5, and Section 10.5.6 apply as if accessors were methods of a corresponding form: A get accessor corresponds to a parameterless method with a return value of the property type and the same modifiers as the containing property. 
    A set accessor corresponds to a method with a single value parameter of the property type, a void return type, and the same modifiers as the containing property. 
    In the exampleabstract class A
    {
       int y;
       public virtual int X {
          get { return 0; }
       }
       public virtual int Y {
          get { return y; }
          set { y = value; }
       }
       public abstract int Z { get; set; }
    }
    X is a virtual read-only property, Y is a virtual read-write property, and Z is an abstract read-write property. Because Z is abstract, the containing class A must also be declared abstract.A class that derives from A is show below:class B: A
    {
       int z;
       public override int X {
          get { return base.X + 1; }
       }
       public override int Y {
          set { base.Y = value < 0? 0: value; }
       }
       public override int Z {
          get { return z; }
          set { z = value; }
       }
    }
    Here, the declarations of X, Y, and Z are overriding property declarations. Each property declaration exactly matches the accessibility modifiers, type, and name of the corresponding inherited property. The get accessor of X and the set accessor of Y use the base keyword to access the inherited accessors. The declaration of Z overrides both abstract accessors — thus, there are no outstanding abstract function members in B, and B is permitted to be a non-abstract class.
      

  2.   

    virtual 是用于修改方法或属性的声明,声明的方法或属性也就是成为虚拟成员,虚拟成员的实现可由派生类中的重写成员更改。在这里也就用到overrider,你查一下帮助文档,讲得很好。关键字“virtual ”,肤浅的理解,请误齿笑。
      

  3.   

    virual是虚函数,overrider是重写父类的virual函数
    当基类得对象调用派生类得方法时,需要在派生类中对基类得虚函数重载.
      

  4.   

    000: // Versioning\versioning.cs
                      001: public class MyBase
                      002: {
                      003: public virtual string Meth1()
                      004: {
                      005: return "MyBase-Meth1";
                      006: }
                      007: public virtual string Meth2()
                      008: {
                      009: return "MyBase-Meth2";
                      010: }
                      011: public virtual string Meth3()
                      012: {
                      013: return "MyBase-Meth3";
                      014: }
                      015: }
                      016:
                      017: class MyDerived : MyBase
                      018: {
                      019: public override string Meth1()
                      020: {
                      021: return "MyDerived-Meth1";
                      022: }
                      023: public new string Meth2()
                      024: {
                      025: return "MyDerived-Meth2";
                      026: }
                      027: public string Meth3() // 系统在这里将会有一个警告,并且将会隐藏方法Meth3()
                      028:
                      029:
                      030: {
                      031: return "MyDerived-Meth3";
                      032: }
                      033:
                      034: public static void Main()
                      035: {
                      036: MyDerived mD = new MyDerived();
                      037: MyBase mB = (MyBase) mD;
                      038:
                      039: System.Console.WriteLine(mB.Meth1());
                      040: System.Console.WriteLine(mB.Meth2());
                      041: System.Console.WriteLine(mB.Meth3());
                      042: }
                      043: }                  输出:
                      MyDerived-Meth1
                      MyBase-Meth2
                      MyBase-Meth3
    以上提供了virtual ,override,new之间的联系与区别,好好分析一下
      

  5.   

    我的简单理解,virtual可被取代,overrider取代已用virtual定义的
      

  6.   

    virtual是c#中的写法,在vb.net中就是overridable了!!
      

  7.   

    liuzxit说得很对!!
    不过这是c#中的用法,c++中没有override关键字,只需要用virtual!!
      

  8.   

    virtual是用在abstract基类中的方法重用的,override是继承类中重用abstract类的方法用的.
      

  9.   

    public class Base //基类
    {
       pulbic virtual void area()
       {...}
    }pulbic class A:Base  //派生类
    {
       pulbic override void area()
       {...}
    }
    pulbic class B:Base
    {
       pulbic override void area()
       {...}
    }public class test
    {
      ....
      Base[] b=new Base[3];
      b[1]=new Base();
      b[2]=new A();
      b[3]=new B();
      //b对象可以访问不同派生类中得area方法
      
    }
      

  10.   

    谁能把virtual和overrider的区别用文字描述比较一下啊
      

  11.   

    virtual是虚方法,申明得时候可以定义,动态绑定,当需要用到基类对象访问派生类得对象的时候可以把基类得对象设置为虚方法也就是加关键字virtual.在派生类中申明对虚函数的重载,要求载什么中加上override关键字.
      

  12.   

    virtual关键字在基类方法定义时使用在派生类中与之对应的是使用override或new关键字,重写或屏蔽基类的虚方法。public class Base //基类
    {
       pulbic virtual void area()
       {...}
    }pulbic class A:Base  //派生类
    {
       pulbic override void area()
       {...}
       //or
       new public void area()
       {...}
    }
      

  13.   

    仔细体会下面这段代码,你就理解了
    using System;
    class A
    {
    public void F() { Console.WriteLine("A.F"); }
    public virtual void G() { Console.WriteLine("A.G"); }
    }
    class B: A
    {
    new public void F() { Console.WriteLine("B.F"); }
    public override void G() { Console.WriteLine("B.G"); }
    }
    class OverrideTest
    {
    static void Main() {
    B b = new B();
    A a = b;
    a.F();
    b.F();
    a.G();
    b.G();
    }
    }