Javase-based constructors (construction methods) and general methods

Source: Internet
Author: User

I. Concept: 1. Construction method

Syntax format:

Modifier class name (parameter type parameter 1, parameter type parameter 2,...) {.....}

The method used to create an object and initialize the properties of the object, called the constructor method, also known as the constructor, and the constructor is defined in the class. If you do not specify a method for constructing a class, the system defaults to creating an argument-free constructor for the class.

2. General methods

Syntax format:

Modifier return value type method name (parameter type parameter 1, parameter type parameter 2,...) {method body;}

is used to calculate the current object data and implement business functions. The method is the object's function, the object's action, and the object's behavior. In short, verbs! The method name is not specified, it is suggested that the first word is a lowercase verb , and the other words are capitalized. The return value must be defined (no return value, declaration return parameter is void)! There are no parameter methods available. method calls are only two ways:the ① object reference calls the ② class name (that is, when the static Class).

Two. The difference between the construction method and the general method

1) The name of the constructor must be the same as the class name, including case.

2) The constructor does not return a value, but it cannot write void, nor can it write a return.

3) constructor parameters: General is the precondition for initializing objects.

4) Call with new! And once the object is established, the constructor runs and runs only once. The general method can be called multiple times.

5) class must have a constructor! This is true, no need to question!

6) If the class does not declare (define) any of the constructors, the Java compiler will automatically insert the default constructor!

7) The default construct is no parameter, the method body is an empty constructor, and the access rights of the default constructor vary with the access rights of the owning class. For example, if the class is modified by public, the default constructor is also with the public modifier.

8) The default constructor is not visible, once you write on the constructor, the default constructor is not, it is written by the custom constructor, even if it is written by the constructor of the empty argument, but also the custom constructor, not the default constructor.

9) If the class declares a constructor, the Java compiler will no longer provide a default constructor. If you do not manually write the parameterless constructor, but call the parameterless constructor, will be an error!

10) constructors can be overloaded and overloaded for convenience, and overloading rules are the same as method overloading rules.

11) The constructor is not inherited! Although it is called the construction method, but in fact it is not often said general method.

12) subclass inherits the parent class, then the child type constructor calls the parameterless constructor of the parent type by default.

13) The subclass constructor must call the parent class constructor, and if the parent class does not have a parameterless constructor, then the parent class has to be called with a parameter constructor. So why must subclasses access the constructor of the parent class?

Because the data subclasses in the parent class can be obtained directly. So when a subclass object is created, you need to see how the parent class initializes the data, so the subclass accesses the constructor in the parent class when the object is initialized.

In summary, at least one constructor in a subclass accesses the constructor in the parent class, and the first row within each constructor in the subclass has an implicit super ().

14) The construction method cannot be called by the programmer, but is called by the system.

Construction Method effect:

             1. In order to initialize the member properties instead of initializing the object, the initialization object is implemented by the New keyword

2. Initializes the object with the new call construction method, which examines the constructor based on the parameter signature at compile time, called Static and compiled polymorphic
(parameter signature: type of parameter, number of parameters and order of parameters)

3. Creating a Subclass object invokes the parent class construction method but does not create the parent class object, just invokes the parent class constructor method to initialize the parent class member property;

15) The difference between the construction method and the general method in operation:
Construction Method:
The construction method initializes the object when it is created, and an object constructs a constructor method that runs only once;
General Method:
The general method is that the object is called to execute, the object is added to the function, the general method can be called by the object multiple times;
sometimes more than one constructor may exist in a class, and each constructor has a different parameter type
multiple constructors can be seen as overloading of methods
match the corresponding constructor according to the parameter type

Javase-based constructors (construction methods) and general methods

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.