C # reload rewrite Reconstruction

Source: Internet
Author: User

C # reload rewrite Reconstruction
Preface

The previous blogs mentioned rewriting and reload. Today, we mainly talk about refactoring and, by the way, compare the three.

Refactoring, rewriting, and overloading

Restructuring is to adjust the program code to improve the quality and performance of the software, so that the program design mode and architecture become more reasonable, and improve the scalability and maintainability of the software. In layman's terms, refactoring is to improve the design of existing code and make the code more compliant with object-oriented rules.

Rewriting occurs in polymorphism. The virtual keyword declares the virtual method, and the subclass implements method rewriting through override. A fuzzy method is declared in the parent class, and no specific behavior is given. In the subclass, rewrite the method in the parent class to instantiate the method and fit the subclass.

Overload. In general, its function is to make more choices. For example, to declare a method in the class, but in actual calls, this method sometimes meets what I want and sometimes does not. For example, if a baby is born, the parents may think about the name of the baby before birth or after birth, if you only use constructors, there is only one method that does not meet my requirements. In this case, method Overloading is required. The previous method is retained, and a method is added. In this example, if the parent married the baby, the original method is used. If the name is not obtained, the method is reloaded.

Here is an example of refactoring. Next, the last match. Now, the Mavericks and lambs register for the competition. What should they do? Let's take a look at how polymorphism works. Now, you can create two more classes, imitate dogs and cats, and inherit Animal. The rest remain unchanged except for the call. After the call is completed, it can run perfectly and have the functions we want. However, the absence of errors does not necessarily mean excellence. We found that there were almost no differences except the sounds of the four animals, which resulted in repeated errors. Since there are duplicates, We need to extract them into Animal. However, Animal is an abstract class and there is no specific call. We will change the call part to another method: getShoutSound. Next, let's implement it.
Class Animal {...... Public string Shout () {string result = ""; for (int I = 0; I <shoutNum; I ++) result ++ = getShoutSound () + ","; return "my name is" + name + "" + result;} protected virtual string getShoutSound () {// "get call", virtual method, let the subclass override, you only need to use it for the inherited subclass, so use the protected modifier. Return "";}}
At this time, the subclass is very simple. For example, we also let the cat call.
Class Cat: Animal {public Cat (): base () {} public Cat (string name): base (name) {} protected override string getShoutSound () {return "meow "}}
The other animals are the same as cats. Just change the call. Although a specific Animal does not have the Shout method, It inherits from Animal. The Shout method of Animal can be inherited by a specific Animal. Of course, the external world can also call this method of a specific Animal. This is refactoring. If we find that the previous Code has errors and duplicates, We Can refactor the Code to make it more effective. To sum up this blog, I wanted to write refactoring directly. However, refactoring, rewriting, and reload all have duplicate words, so I should learn them more easily, refactoring is to modify the structure of the code and optimize it when you find that the Code is not optimal. Rewrite is to define a virtual method. If a subclass is rewritten, it can have different names, while a reload must have the same name. What is its equivalent? My understanding is to give you a default method and another method. You can use the default method or another method, but the two methods have the same name. By summing up this blog, I found that comparative learning can better motivate us to discover, remember, and share something with you.

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.