[Programming Basics]-method overloading, rewriting, refactoring, and basic programming refactoring

Source: Internet
Author: User

[Programming Basics]-method overloading, rewriting, refactoring, and basic programming refactoring

Method overload:

Method overloading refers to defining multiple methods with the same name in a class, but each method must have different parameter types or number of parameters.

By using method overloading, you can meet more situations. For example:

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> class Cat {private string name = ""; public Cat (string name) {this. name = name;} // public Cat () // {// this. name = ""; //} public string Shout () {return "my name is" + name + "meow" ;}</span> </span>

If Cat is not overloaded and there is no parameter, an error is returned when Cat a = new Cat () is instantiated.



Method Rewriting:

First, understand the virtual method: In order for the subclass instance to completely replace Class Members from the parent class, the parent class must declare the member as virtual, this requires that the virtual keyword be added before the returned type of the member is changed.

Then the subclass uses the override keyword to replace the implementation of the parent class with its own implementation.

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> class Animal {public virtual string Shout () // virtual indicates that this method is a virtual method. You can overwrite {return "" ;}}</span> </span>

<Span style = "font-size: 18px;"> <span style = "font-size: 18px;"> class Dog: Animal {public override string Shout () // method override {string result = ""; for (int I = 0; I <shoutNum; I ++) result + = "Wang "; return "my name is" + name + "" + result ;}</span> </span>

Similarly, if subclass implements the implementation of the parent class, what is the difference between method rewriting and direct inheritance:

Inheritance is implicate, which cannot reflect the diversity of sounds. That is, when the method for the parent class needs to be modified in the subclass, it needs to be rewritten using the method.


Refactoring:

Refactoring is to adjust the program code to improve the quality and performance of the software, make the program design mode and architecture more reasonable, and improve the scalability and maintainability of the software.

In the process of software development, there will certainly be some changes. Many factors cannot be found well in the planning stage. Only in the development process can we find some better classification methods, at this time, we should refactor to make the program more complete.


Reload, rewrite, and refactor. It is easier to get a better understanding of these basics .....

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.