C # learning notes series: The difference between new and override

Source: Internet
Author: User

Recently, for the purpose of work, I began to really learn C # And read the textbook C #Programming LanguageDetailed explanation, prepared by Anders hejlsberg. The language is not gorgeous, but it is as rigorous and logical as the compiler.

After reading this article, I found some points that I couldn't quite understand.

The first problem is that there are no obvious tokens in other languages, such as functions and dim in VB. In C #, both variables, attributes, and functions use the same declaration process: Public int something. If parentheses are added, it is a function. Otherwise, if the braces exist, they are attributes. Otherwise, they are variables.

Without these modifiers, there is another problem if the class polymorphism is completed. This concept is one of the most important and hard-to-understand concepts in the class. It seems that this syntax is not explicitly mentioned in the book. However, several important words are found: Virtual, override, and new. The first two have seen it before reading C ++. What is this new? According to the statement in the book and the prompt of Vs, it seems that I have something to imagine with override. But there must be a difference, so write segmentsCodeStudy it.

Use the classic human class as the base class to implement a function walk. Two sub-classes man and woman inherit this class and implement this function walk without any other toggle. Main Function

Human H1 = New MAN ();
H1.walk ();
Human H2 = New WOMAN ();
H2.walk ();

To view the results. If the function in the base class does not have a virtual modifier, and the function in the subclass does not contain any modifier, the compiler reports an error and adds a new prompt. OK. In addition, the two functions show that the base-class walk is executed. This does not seem to be in line with the definition of polymorphism.

Therefore, add virtual to the base class function, and define the function with override and new for the two sub classes respectively, and then execute it again. The first process calls man's walk, which is a correct polymorphism process. The second process calls the human walk.

From this we can see that new adds a new function to the subclass. Apart from the same function name, it has nothing to do with the same name function in the base class. When you use the subclass type to call the function, it runs the function in the subclass. If the type is a base class, the hidden base class function will come to the foreground. Only functions in the base class are defined by virtual, and the functions in the subclass are marked by override can realize the desired multi-state class.

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.