006-Object-oriented constructor (or construction method)

Disclaimer: All my articles are the compilation of online teaching videos, including Mad God Talk, Shang Silicon Valley, Dark Horse Programmer, etc., used as reference materials, without any commercial use, please refer to the majority of netizens, please do not spray ,Thank you. (Note that due to the website, some code characters may have problems. It is recommended that when reading the code, it is best to look at the corresponding picture below)

1. Features of Constructor

1. It has the same name as the class
2. It does not declare the return value type, which is different from the declaration as void
3. It cannot be modified by static, final, synchronized, abstract, or native, and cannot have return statement return values

Second, the role of the constructor

Create an object and initialize the object.
Just as we stipulate that every "person" must take a bath when it is born, we can add the program code to complete the "bath" in the "person" constructor, so every "person" is born The "bath" will be automatically completed, and the program no longer has to tell everyone to "bath" one by one when they are just born.

Three, grammatical format

Modifier class name (parameter list) {
initialization statement;
}
Example:
006-Object-oriented constructor (or construction method)
Create an instance of the Animal class: Animal a = new Animal();
call the constructor to initialize the legs to 4.

Fourth, the classification of the constructor

According to different parameters, constructors can be divided into the following two categories:
1. Implicit no-parameter constructors (provided by the system default)
2. Explicitly define one or more constructors (no parameters, with parameters)
Note:

  • In the Java language, every class has at least one constructor
  • The modifier of the default constructor is the same as the modifier of the class
  • Once the constructor is explicitly defined, the system no longer provides a default constructor
  • A class can create multiple overloaded constructors
  • The constructor of the parent class cannot be inherited by the subclass.
    Exercise:
    1. Add a constructor to the Person class defined above, and use the constructor to set the initial value of all age attributes to 18.
    2. Modify the class and constructor in the above question and add the name attribute, so that the age attribute value and name attribute value of the object are initialized each time a Person object is created.
    006-Object-oriented constructor (or construction method)
    3. Write two classes, TriAngle and TriAngleTest, in which the TriAngle class declares private base length and height, and at the same time declares public methods to access private variables. In addition, provide the necessary constructors for the class. Another class uses these public methods to calculate the area of ​​the triangle.

    Five, constructor overload

    1. The constructor is generally used to create the object and initialize the object at the same time, such as:
    006-Object-oriented constructor (or construction method)
    2. The constructor overload makes the creation of the object more flexible and facilitates the creation of various objects, such as:
    006-Object-oriented constructor (or construction method)
    3. The constructor overloads, the parameter list must be different
    Example:
    006-Object-oriented constructor (or construction method)
    Exercise:
    1. Define the Student class with 4 attributes: String name; int age; String school; String major;
    2. Define 3 constructors of the Studnet class:

  • The first constructor Student(String n, int a) sets the name and age attributes of the class;
  • The second constructor Student(String n, int a, String s) sets the name, age and school attributes of the class;
  • The third constructor Student (String n, int a, String s, String m) sets the name, age, school and major properties of the class;
    3. In the main method, call the objects created by different constructors and output them Attribute value.

    Sixth, summary: attribute assignment process

    Up to now, we have talked about many places where you can assign values ​​to class properties. Now summarize these positions and indicate the order of assignment.
    Location of assignment:
    1. Default initialization 2. Explicit initialization 3. Initialization in the constructor 4.
    Assignment order by means of "Object. Attribute" or "Object. Method" : 1-2-3-4

Guess you like

Origin blog.51cto.com/12859164/2546352