"Fundamentals of Programming"--method overloading, rewriting, refactoring

Source: Internet
Author: User

Method overloads:

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

With the method overload, more can be satisfied more situations. Like what:

<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 = "Nameless";        } public        string Shout ()        {            return "My name is" + name + "Meow";        }    } </span></span>

When the cat is not overloaded without parameters, when instantiated: Cat A=new cat (); will be an error.



Method overrides:

First understand the virtual method: In order for an instance of a subclass to completely replace a class member from the parent class, the parent class must declare the member as virtual, which requires the virtual keyword to be added before the return type of the member is changed.

The child class then 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 ()//has virtual means that this method is a virtual method, can be overridden by the quilt class        {            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, subclasses implement the implementation of the parent class, and the method overrides and direct inheritance are different:

Inheritance is intact, all copied, unable to reflect the diversity of calls. That is, when a method for a parent class is required to be modified in a subclass, it is necessary to use a method rewrite.


Refactoring:

Refactoring (refactoring) is to improve the quality and performance of the software by adjusting the program code, making its program design pattern and architecture more reasonable, and improving the expansibility and maintainability of the software.

Software in the development process, there will be certain changes, many factors are not well found in the planning stage, only in the development process, will find some better classification methods, then should be reconstructed, so that the program is more perfect.


Overloading, rewriting, refactoring. It's easier to understand when you compare them .... These foundations should be well grasped .....

"Fundamentals of Programming"--method overloading, rewriting, refactoring

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.