Constructor and instantiation

Source: Internet
Author: User

1. The object is constructed. You cannot create a new object without calling the constructor.
2. Each class, including an abstract class, must have a constructor. Be sure to keep this in mind. However, each class must have a constructor, which does not mean that the programmer must input it.
3. the constructor does not have any return types.

Constructor rules
1. constructors can use any access modifier, including private. (Private constructor means that only the code of this class can instantiate this type of object. Therefore, if the private constructor class wants to allow instances of this class, the class must provide static methods or variables that allow access to instances created from this class );
2. the constructor name must be the same as the class name.
3. the constructor must not have a return type.
4. It is legal (but stupid) to make the method and class have the same name, but this does not make it a constructor. If the returned type is displayed, it is a method rather than a constructor;
5. If you do not enter a constructor in the class code, the compiler automatically generates the default constructor;
6. default constructor always has no parameter constructor.
7. Each constructor must call the overload Constructor (this () or call the parent Constructor (Super () as its first statement.
8. if you do enter a constructor (not dependent on the default constructor generated by the compiler) without calling super (), the compiler inserts a pair of super ().
9. Super () can be called without parameters, or include parameters passed to the parent constructor;
10. You cannot call an instance method or access an instance variable unless the parent constructor is running.
11. You can access static variables and methods, although they can only be used as part of calling super () or this. (Super (animal. dothings ()));
12. abstract classes have constructors, which are always called when a specific subclass is instantiated.
13. The only way to call a constructor is to call it from another constructor.

Public student (INT age, string name)
{
This. Age = age;
This. Name = Name;
}

Constructor:
1. The method and class name are the same
2. No return value
3. used to initialize objects
4. called when the class is instantiated
5. When the class has no constructor, a constructor without parameters is automatically provided.

Execution sequence of the constructor: first parent class and then Child class.

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.