Page 5 Constructor, default constructor, default constructor

xiaoxiao2021-03-06  39

2.3 In-depth exploration functions:

2.3.1 Constructor, default constructor, default constructor

For the above example, it can have the most of the work, but it is still imperfect, there are many details waiting to we will improve! Perhaps some classmates have noticed that when I created this object of "jingwei", all the properties of this object are empty, that is, the name of this object is undecided, age is undecided, sex is not fixed , Salary is undecided, lunch has not been determined. And we want to add these attributes, but also use object to call the corresponding method, go to one by one! God, this is too much trouble! Is there any good way to complete the operation of the property assignment while we create an object? Oh, it should be said to be the initialization of attributes? Of course, there is no problem, this requires a so-called constructor!

The constructor is the most special function in the class, which is just the opposite of the designer function!

From the feature: 1. It is the only function in the programming language without the type of return value.

2. Its name and class name must be exactly the same.

3. It must be declared as public (public) type

4, you can overload the constructor.

5. It is automatically called when you create an object.

From functional: 1. It is initialized to attributes in the class.

In fact, we don't have your own constructor for the above programs. However, in this case, the system will automatically define a "default constructor". He will automatically assign the numerical variable to 0, assign the Boolean variable to false, etc. (, in C , the default constructor does not initialize its members). If the programmer defines the constructor, the system will not add a default function to your program. (Here, we advocate yourself to define the constructor, not the default constructor of the system)

Still look at an example! This is more clear!

//employee.java public class employee {private String name; // employee name private int age; // employees aged private char sex; // staff gender private float emolument; // staff salaries private boolean lunch; // staff lunch / / ... Wait Public Employee () {// This is the "default" constructor name = "jw"; // Set the employee name AGE = 20; // Set employee age SEX = "m"; // Set employee gender Emolument = 100; // Set employee salary lunch = false; // Set employee lunch} public void heater () {// This method is used to process lunch lunch = true;} // ..., etc.};

This way, while we create "jingwei" objects, all of its properties are also initialized! Obviously, this greatly improves working efficiency, but it still does not meet the requirements. Think about it, what happens if we now create this type of second object? Tell you, in addition to the "name" of the object (this name is not the name in the object property, but the name of the object itself) is not the same, all of its "property value" is the same! For example: Now we create a second object flashmagic, but I will find all the properties of this object and all the properties of the object of Jingwei are exactly the same. And we can only change write properties with objects! Obviously, this method is not good! We need a method to give "We want" when creating an object. I believe that you also see it, the default constructor is unable to force. What we need is a constructor with parameters. When you create an object, we pass the parameters to the constructor so that the above features can be completed! No mouth, or come to see an example:

//employee.java public class employee {private String name; // employee name private int age; // employees aged private char sex; // staff gender private float emolument; // staff salaries private boolean lunch; // staff lunch / / ... Wait Public Employee (String N, Int A, CHAR S, FLOAT E, Boolean L) {// Look at this constructor Name = n; // Set the employee name AGE = a; // Set employee SEX = S; // Set employee gender emolument = E; // Set employee salary lunch = L; // Set employee lunch} public void heater () {// This method is used to process employees lunch = true;} // ……and many more};

In this way, we can give him the value we want, it is obvious that this can be more convenient. Oh, right! Haven't told you how to create it! Haha, turn a few pages forward, you will see this sentence:

Jingwei = new Employee (); this is an object, and we change it to

Jingwei = New Employee ("Jingwei", 20,

转载请注明原文地址:https://www.9cbs.com/read-62409.html

New Post(0)