C # overloading Rewrite refactoring

Source: Internet
Author: User
Tags repetition

Preface

Some of the previous blogs have said rewriting and overloading, and today it is mainly about refactoring, by the way, by comparing the three.

Refactoring , overriding, overloading

Reconstruction is to improve the quality and performance of the software by adjusting the program code, so that the design pattern and architecture of the program are more reasonable, and the expansibility and maintainability of the software are improved. in layman's terms, refactoring is about improving the design of existing code to make code more compliant with object-oriented rules.

Whereas overrides appear in polymorphism, the virtual keyword declares a virtual method, and subclasses implement the method rewrite by overriding. A fuzzy method is declared in the parent class, and does not give specific behavior, in subclasses, by overriding this method in the parent class, let this method instantiate, to fit the subclass.

Overload, in layman's terms, its role is to allow us to choose more, such as the declaration of a method in the class, but in my actual call, this method is sometimes in line with what I want, sometimes do not meet, such as a baby born, parents may be born before the baby's name, may also not think well after birth, If only the constructor, only one method, can not meet my requirements, this time to use the method overload, the previous method to retain, and then add a method, and then this example, if the parents to the baby to marry the name, then, with the original method, if not named, will be overloaded with the method.

Examplefirst of all, the refactoring example. and then the last game. Now the calf and the lamb to sign the game, how should be done? Let's take a look at how we do it in polymorphism.
now, you can create two more classes, mimicking dogs and cats, and inheriting animal. Other than the call remains unchanged, so that, after completion, can be perfectly run, and have the function we want. However, no error does not necessarily represent excellence, and we find that there are almost no differences except for the four different kinds of animals, which makes a repetition of the problem. Since there is repetition, it is necessary to put the repetition of the extraction into the animal, but, animal is an abstract class, no specific calls, we will call part of the change to another method: Getshoutsound. Let's take a look at the next step.
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 subclass rewrite, only need to use for inheriting subclass, so with protected modifier.        return "";    }}
the subclass is very simple at this time. For example, we also let the cat to bark.
Class cat:animal{public    Cat (): Base ()    {} public    Cat (string name): Base (name)    {}    protected Override string Getshoutsound ()    {        return "Meow"    }}
The rest of the animals are like cats, so they can be changed. Although the specific animal does not have the shout method, but it inherits from the Animal,animal shout method can be the concrete animal inheritance, of course, the outside world also can call the concrete animal this method. This is refactoring, discovering that the previous code has errors, repeats, and so on, refactoring the code to make it a better completion feature. Summarythis blog, originally wanted to directly write refactoring, but, refactoring, rewriting, overloading, there is a heavy word, more should be compared to learn them, in short, refactoring is when you find the code of the shortcomings, to modify its structure, to optimize it. Rewrite is to define a virtual method, subclass to rewrite, it can be different name, and overload must be the same name, what is he equivalent? My understanding is, give you a default method, and give you another method, you can use the default method, you can use the other one, but the two method names are the same. by summarizing this blog, I found that comparative learning is better for us to discover, to remember something, to share with you.


C # overloading Rewrite refactoring

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.