New modifier and Polymorphism in C #

Source: Internet
Author: User

The new Keyword can be Used as an operator to create an object or modifier. When Used as a modifier, the official document explains this as: Used to hide an inherited member from a base class member. chinese means to hide the members inherited from the base class. So how can we understand what "hidden is? I think that hiding here refers to hiding the member inherited from the base class. It can be understood that although the subclass inherits the Member from the base class, however, the member is invisible to the subclass, or the subclass does not think that the member is inherited from the parent class, but does not have any relationship with the parent class. Assume there is the following code: public class Program {static void Main (string [] args) {Son s = new Son (); s. methodB () ;}} public class Father {public virtual void methodA () {Console. writeLine ("Father. methodA ") ;}public virtual void methodB () {methodA () ;}} public class Son: Father {public new void methodA () {Console. writeLine ("Son. methodA ") ;}} when s. when methodB ();, it will run the methodA inherited from Father in s, but the program finds that the Son class does not R inherits the methodA method (although there is a methodA method in the Son class, the program does not think this method is inherited from Father ). Therefore, in this case, the program searches for the base class closest to the Son Class Based on the inheritance chain, finds Father, and then calls methodA in the Father class. Therefore, the program outputs Father. methodA. If you change new to override, Son. methodA is returned. Therefore, we can conclude that both override and new call methods of this type based on the object runtime type. This method is called when the method is modified by override. However, when the method is modified by new, it is considered that the method has not been inherited. Instead, it finds the method of the base class closest to the object based on the inheritance chain. When inheriting virtual functions, whether using the new modifier or override, it is a manifestation of polymorphism. The concept of polymorphism is simply that object A shows the behavior and Properties of object B. In computer science, polymorphism is a feature of programming languages. It allows different types of data to be operated through a unified interface. Polymorphism is usually divided into polymorphism during compilation and Runtime polymorphism. The running polymorphism is the operation that is performed only when the system is running. Both new and override are used to determine the method to be called during running. Let's look at the following example to better understand the relationship between new and override and Polymorphism: public class Program {static void Main (string [] args) {string input = Console. readLine (); Person p = null; if (input = "0") {p = new GrandFather ();} else if (input = "1 ") {p = new Father ();} else if (input = "2") {p = new Son ();} p. methodA () ;}} public class Person {virtual public void methodA () {Console. writeLine ("Person. methodA ") ;}} public class Gra NdFather: Person {override public void methodA () {Console. writeLine ("GrandFather. methodA ") ;}} public class Father: GrandFather {public override void methodA () {Console. writeLine ("Father. methodA ") ;}} public class Son: Father {public new void methodA () {Console. writeLine ("Son. methodA ") ;}} www.2cto. comp is declared as an object of the Person class, but p is different in runtime according to different input parameters. When the input is 0, p is displayed as the GrandFather class. It calls the inherited methodA method in the GrandFather class and outputs the GrandFather. methodA when 1 is input, p is represented as the Father class. Call the methodA method inherited from the Father class and output the Father. methodA when the input is 2, p is represented as Son class, which calls the inherited methodA method in Son class, but because the methodA method in Son class is modified by new, therefore, the inherited methodA methods in the Son class are hidden and invisible. Therefore, according to the inheritance chain, the methodA method in the Father class is called and therefore the Father method is output. methodA.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.