C # Object-Oriented Programming-method overloading for the warm and newest Learning Series (8)

Source: Internet
Author: User

Preface

We have said that the constructor initializes the class. We hope that the new kitten has a name called "Mimi". When it is called, it can say "my name is Mimi ", the constructor is required now. The following Code does not contain any green code, but the question is? If we didn't give the kitten a good name in advance, we wouldn't be able to create an instance. This is a normal phenomenon. Some parents just gave birth to a child, it would be very normal that they didn't get a good name, the following code does not contain green code. If you write Cat cat = new Cat (), the system reports the error "Cat method does not use any overload of 0 Parameters, the reason is that you must give a name to the kitten. If you do not need a name, you can use the method to reload it.

Method Overloading

Multiple methods with the same name can be created, but different parameter types are required for these methods.

During method overloading, the two methods must have the same name, but the parameter type or number must be different.

What are the benefits of method overloading?

The advantage of method Overloading is that it adds features without changing the original method.

Instance

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace _ 7_ConstructedFunction
{
Class Program
{
Static void Main (string [] args)
{
Cat cat = new Cat ("Mimi ");
Console. WriteLine (cat. Shout ());

// A kitten can be generated without a name

Cat cat2 = new Cat ();

Console. WriteLine (cat2.Shout ());
Console. ReadKey ();
}
}

Class Cat
{
Private string _ name = "";

// Reload the constructor by adding this green code

Public Cat ()
{

This. _ name = "anonymous ";
}
Public Cat (string name)
{
This. _ name = name;
}

Public string Shout ()
{
Return "my name is" + _ name;
}
}
}

Running Effect

  

Thoughts

In the above example, some kittens use constructor with the string parameter after their names are ready. If some kittens do not have a name, they use constructor without parameters. This achieves the purpose of extension.

If we need to clarify the Cat's surname and name, we can reload a constructor public Cat (string strFirstName, string strLastName)

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.