Reference: new, virtual, and override

Source: Internet
Author: User

Let's take a look at the following section.Program:

  1. /// <Summary>
  2. /// Parent class
  3. /// By Zhou Gong
  4. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  5. /// Date: 2008-09-01
  6. /// </Summary>
  7. Public class father
  8. {
  9. Public void run0 ()
  10. {
  11. Console. writeline ("father. run0 ");
  12. }
  13. }
  14. /// <Summary>
  15. /// Subclass
  16. /// By Zhou Gong
  17. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  18. /// Date: 2008-09-01
  19. /// </Summary>
  20. Public class son: Father
  21. {
  22. Public void run0 ()
  23. {
  24. Console. writeline ("son. run0 ");
  25. }
  26. }
  27. Class Program
  28. {
  29. Static void main (string [] ARGs)
  30. {
  31. Father [] fatherlist = new father [2];
  32. Fatherlist [0] = new father ();
  33. Fatherlist [1] = new son ();
  34. Fatherlist [0]. run0 ();
  35. Fatherlist [1]. run0 ();
  36. }
  37. }

 

The running result of the program is:
Father. run0

Father. run0

 

A slightly careful friend may find thatSonClassRun0There is a brown wavy line below the method. When we place the mouse over the underline, we will see the following prompt(This warning can also be seen in the "output" Window of the program during program compilation.):

"Methoddemo. Son. run0 ()" hides the inherited member "methoddemo. Father. run0 ()". If it is intentionally hidden, use the keyword new.

 

Then let's use the run method of the second version, which is called run1 (). The difference with the first version is that the same parameter of the subclass is the same (the method name is the same, the number of parameters is the same as the parameter order.) A new keyword is added before the method,CodeAs follows:

  1. /// <Summary>
  2. /// Parent class
  3. /// By Zhou Gong
  4. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  5. /// Date: 2008-09-01
  6. /// </Summary>
  7. Public class father
  8. {
  9. Public void run1 ()
  10. {
  11. Console. writeline ("father. run1 ");
  12. }
  13. }
  14. /// <Summary>
  15. /// Subclass
  16. /// By Zhou Gong
  17. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  18. /// Date: 2008-09-01
  19. /// </Summary>
  20. Public class son: Father
  21. {
  22. Public new void run1 ()
  23. {
  24. Console. writeline ("son. run1 ");
  25. }
  26. }
  27. Class Program
  28. {
  29. Static void main (string [] ARGs)
  30. {
  31. Father [] fatherlist = new father [2];
  32. Fatherlist [0] = new father ();
  33. Fatherlist [1] = new son ();
  34. Fatherlist [0]. run1 ();
  35. Fatherlist [1]. run1 ();
  36. }
  37. }

The running result is as follows:

Father. run1

Father. run1

 

After the New Keyword is added, the running result of the program is not changed. That is, in C #, if there is a method with the same name as the parent class in the subclass, C # Will help you add a new keyword before the subclass method.

 

Let's write the run method of the third version, namely run2 (). Unlike the first version, a virtual keyword is added before run2 () of the parent class, this indicates that this is a virtual method. The run2 () of the subclass is different from the first version (run0 () to run2.

The program code is as follows:

  1. /// <Summary>
  2. /// Parent class
  3. /// By Zhou Gong
  4. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  5. /// Date: 2008-09-01
  6. /// </Summary>
  7. Public class father
  8. {
  9. Public Virtual void run2 ()
  10. {
  11. Console. writeline ("father. run2 ");
  12. }
  13. }
  14. /// <Summary>
  15. /// Subclass
  16. /// By Zhou Gong
  17. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  18. /// Date: 2008-09-01
  19. /// </Summary>
  20. Public class son: Father
  21. {
  22. Public void run2 ()
  23. {
  24. Console. writeline ("son. run2 ");
  25. }
  26. }
  27. Class Program
  28. {
  29. Static void main (string [] ARGs)
  30. {
  31. Father [] fatherlist = new father [2];
  32. Fatherlist [0] = new father ();
  33. Fatherlist [1] = new son ();
  34. Fatherlist [0]. run2 ();
  35. Fatherlist [1]. run2 ();
  36. }
  37. }

Let's take a look at the running effect of the program:

Father. run2

Father. run2

 

The program running effect is no different from the first one, but this time the run2 () method of the Child class (son) has the same warning as the first version of the run method (run0: "methoddemo. son. run2 () "hides the inherited member" methoddemo. father. run2 ()". To rewrite this implementation for the current Member, add the keyword override. Otherwise, add the keyword new.

 

Let's write the run method of the fourth version, which we call run3 (). The modifier of run3 () in the parent class remains unchanged (still virtual, virtual method), and the run3 () method in the Child class son has an override modifier compared to the third version (the brown wave line warning we are familiar with this time is gone ). The Code is as follows:

  1. /// <Summary>
  2. /// Parent class
  3. /// By Zhou Gong
  4. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  5. /// Date: 2008-09-01
  6. /// </Summary>
  7. Public class father
  8. {
  9. Public Virtual void run3 ()
  10. {
  11. Console. writeline ("father. run3 ");
  12. }
  13. }
  14. /// <Summary>
  15. /// Subclass
  16. /// By Zhou Gong
  17. /// Starting address: http://blog.csdn.net/zhoufoxcn/archive/2008/09/02/2864429.aspx
  18. /// Date: 2008-09-01
  19. /// </Summary>
  20. Public class son: Father
  21. {
  22. Public override void run3 ()
  23. {
  24. Console. writeline ("son. run3 ");
  25. }
  26. }
  27. Class Program
  28. {
  29. Static void main (string [] ARGs)
  30. {
  31. Father [] fatherlist = new father [2];
  32. Fatherlist [0] = new father ();
  33. Fatherlist [1] = new son ();
  34. Fatherlist [0]. run3 ();
  35. Fatherlist [1]. run3 ();
  36. }
  37. }

The program running result is as follows:

Father. run3

Son. run3

 

This time we found that the running results of the program were different from those of the previous three times. Although the declared object types are both father classes (the father classes are naturally referenced by the father classes ), however, when instantiating the second element of an array, the son class constructor is called, that is, the Child class of a father class is instantiated. (We know that child classes can be treated as parent classes, the relationship between them is a, and vice versa ),That is to say, the reference type of the second element in the fatherlist array is father, but its instance type is indeed son type.. During running, the method of the reference type is not called according to our reference type (the reference type is father type, instead, it calls the method of the instance to which the reference type points.

 

Why are these phenomena? Two concepts are mentioned here:Early binding and late binding). This term appears in the base class and derived class with an inheritance relationship. They define a method with the same name as the same parameter at the same time.

Early binding: during compilation, you have determined which method of the base class or derived class to run in the future. During code compilation, the methods defined in the reference type are determined based on the reference type, that is, the methods of the base class. This method is highly efficient.

Late binding: only when running can you decide which method of the base class or derived class to run. When running, relevant methods will be called based on the actual type rather than the reference type, which is determined by what type of object we have new. That is, even if we use a new sub-class son instance of the father class, no matter whether we use the reference of the father class to point to this son instance, the son method is still called when the method is called, instead of the father class method with the same name.

As we have seen above, in order to realize late binding, C # introduces two keywords virtual and override. Different from Java, every method in Java is a virtual method, that is, when running, JVM automatically checks whether the referenced type is consistent with the actual type (no matter what, there is an equal or inheritance relationship between the reference type and the actual type to satisfy the is a relationship.) If the method defined in this type is always executed; if they are inconsistent, the system checks whether the actual type of the reference has the same name as the parameter method. If yes, the system runs the same name and Parameter Method of the actual type. This will bring about a problem: type check will be performed every time you run it, which will lead to a certain amount of performance consumption. In C #, all methods are non-virtual if they are not displayed and specified.For non-virtual methods, the CLR does not check the type when running, but directly runs the methods defined in the referenced type, even if the reference actually points to a derived class of the reference type, and there is a method with the same name as the parameter in the derived class, the method defined in the derived class is not run. In this case, the methods in the derived class hide the methods in the base class.

However, if the declared method is a virtual method in the base class, the CLR checks the type during running. If the reference type is found to be different from the actual object type, the system checks whether the methods in the base class are overwritten in the derived class. If yes, the system runs the methods in the derived class instead of referencing the methods in the type.

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.