what is “public new virtual void Method()” mean?

前端 未结 4 1419
悲&欢浪女
悲&欢浪女 2020-12-03 14:07

when use new virtual key words to decorate the method? what is the affection? Like define an interface, and add a class to inherit the interface. but use the new virtual to

相关标签:
4条回答
  • 2020-12-03 14:52

    new modifier

    http://msdn.microsoft.com/en-us/library/435f1dw2.aspx

    When used as a modifier, the new keyword explicitly hides a member inherited from a base class.

    This means that the method does not override the virtual base class method, but it still takes precedence when called on an instance of the derived class. In other words, the new method only affects a variable of the derived class, not the base class.

    virtual modifier

    http://msdn.microsoft.com/en-us/library/9fkccyh4.aspx

    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.

    This means that the method can be overriden in a derived class. When you call a virtual method on a base class variable which holds an instance of the derived class that has overridden the virtual method, the derived class implementation is called. This is the opposite of the behaviour of the new keyword.

    0 讨论(0)
  • 2020-12-03 14:54

    This is called method hiding. You use this when you need to provide your own implementation for a method that cannot be overridden. Because PrinterOne.Print is not a virtual method, it cannot be overridden. Instead, the new keyword is used to create a identical method signature that hides the original method. The new method will be used instead. Adding the virtual keyword to this, allows your new method to be overridden by deriving classes.

    Your new method that hides the original will only be invoked if you call it through the defining container (eg. PrintTwo). Calling it by the interface calls the original method. Mind you, that the method was never removed or replaced so the original implementation still exists by accessing the interface directly.

    0 讨论(0)
  • 2020-12-03 14:55

    new and virtual are two (mostly-) unrelated keywords.

    new means it shadows the base method.
    virtual allows subclasses to override it.

    Calling the method through the interface results in the base method being called, since the base method is not virtual and the derived classes don't explicitly re-implement the interface (which would cause the method to be re-mapped)

    0 讨论(0)
  • 2020-12-03 15:06

    The new keyword used like this is member hiding.

    I have never seen it used in conjunction with the virtual keyword, mind you. It is simply allowing types that derive from PrinterTwo to override the Print method implementation.

    The new keyword used this way allows a type to hide the members of base types, but only if you are using a variable of the type itself.

    For example, if you were to do:

    PrinterOne one = new PrinterTwo();
    one.Print();
    

    It would not call the method in PrinterTwo as it is not part of the inheritance chain.

    As for when you would do this... when you really, really need to for some odd reason that I can't think of (reflection maybe?) and you cannot edit the code in PrinterOne.

    Personally, I wouldn't ever do this.

    As for why the output is printer one... calling IPrinter.Print will call against the type it is defined on (PrinterOne in this case), which will put you back in my above example about the new keyword being ignored unless you talk to the type that features it.

    Basically, using IPrinter is analogous to using PrinterOne in my small example above.

    To solve the problem, make the PrinterOne method virtual and completely remove the use of new virtual in PrinterTwo.

    0 讨论(0)
提交回复
热议问题