C #: Method overload, constructor, and overload constructor Summary

Source: Internet
Author: User

Method overloading makes the same function applicable to various types of data. It declares more than two methods with the same name, to achieve the same handling method for different data types, reload requirements 1. Reload method names must be the same 2. Reload method, the number or type of parameters must be different. If we define swap (ref int a, ref intb), this function is used to exchange the values of Two integer variables, but does not process floating point data, we are defining a swap (ref flot a, ref flot B), so that the swap method can realize the exchange of integer variable values, you can also implement Floating Point Data Exchange (the system will decide to call the appropriate method based on the data type). The main function of the constructor is to initialize an object when creating an object (declaring an object. A class definition must have at least one constructor. If no constructor is declared when a class is defined, the system will provide a default constructor. For example, you may better understand it. The result is: If you want to set the object data member to a specified value when creating an object, you need to specifically declare the constructor. Requirements for declaring constructor: 1. the constructor cannot have a return type. 2. The constructor name must have the same name as the class name. The constructor is usually used to initialize data members when creating an object. Therefore, the constructor must use a form parameter. Public Student (string ID, int Age) {id = ID; age = Age;} because the preceding constructor has parameters, the system does not provide default constructor. Therefore, when creating an object, the actual parameters must be given according to the declared constructor parameter requirements. Student s1 = new Student ("90090", 22); the New keyword is actually a call to the constructor. If the parameter names used to declare the constructor are the same as the class data member names, the class data member names used in the constructor must have the this Bootstrap Public student (string id, int age) {This. id = id; This. age = age;} the keyword "this" refers to the created object. It is the reference parameter of the object automatically passed to the constructor when the object is declared. The overloaded constructors and methods can be reloaded. The main purpose of the overload constructor is to provide more flexibility for object creation and meet different needs during object creation. In the above example, if you only want to change the age, the overload constructor Student only needs to have a parameter age. The virtual method declares the name of the Public new method (parameter list) of the derived method with the same name as the base class {} declares the Publicvirtual method name in the Declaration format (parameter list) in the virtual method base class) {} The Declaration format Publicoverride method name (parameter list) in the derived class {} calls the base class method to declare a method with the same name as the base class in the derived class, also known as method overload. After a derived class reloads the base class method, if you call a method of the same name as the base class, use the base keyword. Declare abstract classes and abstract METHODS Public abstractclasse class name {public abstract return type method name (parameter list);} overload abstract method Public override return type method name (parameter list)

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.