Function of constructor in C,

Source: Internet
Author: User

Function of constructor in C,
Function of constructor in C #

In common: all objects are instantiated. The default structure of the initialization data means that all classes inherit the construction method of the null parameter from the ancestor object. You do not write or write the null parameter construction to exist, the construction of parameters is generally self-written, and there is no write, it serves the same as the null parameter, but it can have parameters, here is an example to illustrate that there is a class Monitor which has the property String height; Stringwidthpublic Monitor () {} public Monitor (String height, String width) {} There is a construction method with parameters for an empty parameter. In the main method, I initialize the Monitor monitor = new Monitor () action. This construction only creates the Display object, its attributes have no value, and you can manually assign a value to it, for example, monitor. height = 100; monitor. width = 200; it is different to use parameters. Many things are missing: Monitor monitor2 = new Moni Tor ("100", "200"); here, I just need to put the parameters that I want to put in directly to assign values to my attributes. Does it save a lot of trouble? This is its advantage! The answer is complete!
Constructor is a special method. It is mainly used to initialize an object when an object is created, that is, to assign an initial value to the object member variable. It is used together with the new operator. A special class can be used in the statement of creating an object. there are multiple constructors that can distinguish them from other methods based on the number of their parameters or different parameter types. the name of the constructor must be exactly the same as the class name, while the general method cannot be the same as the class name. 2. the constructor is mainly used to define the initialization status when the class object is created. it does not return values and cannot be modified using void. this ensures that it does not have to return anything automatically, and there is no choice at all. other methods return values. even if the return value is void, although the method body itself does not automatically return anything, it can still let it return something that may be insecure. 3. A constructor cannot be called directly. It is automatically called only when an object is created through the new operator. Generally, a method is called when the program executes it. 4. when defining a class, the constructor of the class is usually displayed, and the initialization work specified in the function can also be omitted. the Java compiler will provide a default constructor. this default constructor does not contain parameters. the general method does not have this feature.
In short, constructor is used to create objects. It is specialized in instantiating objects. Definition: class A {public A () {}} use: A a = new A (); // here, A () is the constructor. Note that if no constructor is defined in the class, the compiler will automatically generate an empty constructor, and each member of the class will assign the default value. However, if only one private constructor is defined in the class, the class cannot be instantiated. Class A {string a; private A () {}// this class cannot be instantiated} if we define A constructor with parameters, but do not define a constructor without parameters, parameters must be provided when the class object is instantiated. Class A {string a; public A (string str) {a = str ;}} the object A a = new ();
Constructor can be divided into dynamic constructor and static constructor. When a dynamic constructor is a class or structure instantiation, it allocates memory space for it to complete initialization of the member variables in the class or structure. A dynamic constructor can be overloaded. That is, the same constructor name has multiple constructor types and numbers. There is no essential difference between a constructor with parameters and a constructor without parameters. A constructor with parameters can control the input variables or directly initialize the member variables, you can initialize the default value for the member variable in the constructor. A static constructor cannot carry parameters. That is, only one static constructor is allowed in a class. It is called before the first access to a static member or the first instantiation of an object of a dynamic class.
Category: C #

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.