Object-oriented constructors (which allow programs to initialize something before they run), function (method) overloading

Source: Internet
Author: User

1 constructors

Constructor Features:
1. Special: There is no return type, the function name must be the same as the class name.
2. The timing of execution is special: executing when the class is instantiated is the first function that can be used to initialize the data.

all objects have a default construct (not written), and the function has no arguments, and the class itself has the same name (the name must be the same). A class definition can contain several constructors that have different signatures that code can use to instantiate an object. Constructors with parameters are typically used to provide initial values for the data stored in the object.

The constructor method is used to create the object and initialize the object (assigning values to each property in turn).

When you define a construction method, the name of the constructed method must be the same as the class name, with no return value of void or write.

You can have parameters that can be overloaded (multiple parameters with different constructors).

If you do not specify a constructor, the class defaults to a parameterless constructor, and if a constructor is specified, no more default parameterless constructors are required to write on their own.

Defining constructors

Class person{
public string name;
Public person (string name) {
This. name = name;
}
}

Initializing properties through constructors

Person hei = new person ("small black"); At this time p.name = "small Black";

2. Method (function) overloading

method overloading is another way to implement polymorphism. with method overloading, a class can have multiple methods with the same name, and the different number of arguments passed to them determines which method to use. for example, for a drawing class, it has a draw () method for drawing or outputting text, and we can pass it to a string, a rectangle, a circle, and even the initial position of the drawing, the color of the graphic, and so on. For each implementation, only a new draw () method can be implemented without a new name, which greatly simplifies the implementation and invocation of the method, and programmers and users do not need to remember many of the method names, just pass in the corresponding parameters.

Because a class can contain instructions on how operators operate, you can use an operator with an object instantiated from a class.

We write code for overloaded operators and use them as part of the class definition, which acts on this class. You can also overload operators to handle different classes in the same way, where one (or two) class definition contains code that achieves this purpose.

Note: You can only overload an existing C # operator in this way, and you cannot create a new operator.

The condition of the function overloading;
1. Function names must be the same
2. function requires different number of parameters
3. If the function requires the same number of parameters, the type is different

  

Object-oriented constructors (which allow programs to initialize something before they run), function (method) overloading

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.