2.3 In-depth discussion of functions:
2.3.1 Constructor, default constructor, default constructor
For the above example, it can accomplish most of the work, but it is still incomplete, and there are many details waiting for us to improve! Perhaps some students have noticed that when I create the object "jingwei", all the properties of this object are empty, that is, the name of this object is undetermined, the age is undetermined, the gender is undetermined, the salary is undetermined, and the lunch is undetermined. If we want to add all these properties, we must use the object to call the corresponding methods and modify them one by one! Oh my God, this is simply too troublesome! Is there any good way to complete the assignment of attributes while we create an object? Oh no, it should be said to be an initialization of the attribute? Of course there is no problem, this requires the so-called constructor! A constructor is the most special function in a class, and it is exactly the opposite of the function of a destructor! Features: 1. It is the only function in the programming language that does not have a return value type. 2. Its name must be exactly the same as the name of the class. 3. It must be declared as a public type 4. The constructor can be overloaded. 5. It is automatically called when creating the object. Functionally: 1. It initializes properties in the class. In fact, for the above program, we do not define the constructor ourselves. However, in this case, the system will automatically define a "default constructor" for us. He will automatically assign numeric variables to 0, assign boolean row variables to false, etc. (but in C++, the default constructor does not initialize its members). If the programmer defines the constructor, the system will no longer add a default constructor to your program. (Here, we advocate defining the constructor yourself, rather than using the system's default constructor) Let’s see an example! This is clearer!
//
public class employee{
private String name; //employee name
private int age; //employee age
private char sex; //Employee gender
private float emlument; //employee salary
private boolean lunch; // Staff lunch
//……etc
public employee(){ //This is the "default" constructor
name = "jw"; //Set employee name
age = 20; //Set the employee age
sex = "M"; //Set the employee gender
emulument = 100; //Set employee salary
lunch = false; //Set employee lunch
}
public void heater(){ //This method is used to process employees' lunches
lunch = true;
}
//……etc
}; In this way, while we create the "jingwei" object, all its properties are also initialized! Obviously, this greatly improves work efficiency, but it still does not meet the requirements. Think about it, what would happen if we now create a second object of this type? Let me tell you that except for the "name" of the object (this name is not the name in the object attribute, but the name of the object itself), all its "attribute values" are the same! For example: Now we create the second object flashmagic, but I will find that all the properties of this object are exactly the same as all the properties of this object. And we can only change the writing attributes using the object method! Obviously, this method is not very good! We need a way to assign "the value we want" to the object's properties when creating them. I believe you have seen it too, and the default constructor seems powerless. What we need is a constructor with parameters. When creating an object, we pass the parameters to the constructor, so that the above functions can be completed! It’s no point to say it, let’s see an example:
//
public class employee{
private String name; //employee name
private int age; //employee age
private char sex; //Employee gender
private float emlument; //employee salary
private boolean lunch; // Staff lunch
//……etc
public employee(String n,int a,char s,float e,boolean l){ //Look at this constructor
name = n; //Set employee name
age = a; //Set the employee age
sex = s; //Set the employee gender
emulument = e; //Set employee salary
lunch =l; //Set employee lunch
}
public void heater(){ //This method is used to process employees' lunches
lunch = true;
}
//……etc
}; In this way, while creating the object, we can assign the value we want to it, which is obviously much more convenient. Oh, right! I haven't told you how to create it yet! Haha, if you turn a few pages forward, you will see this sentence: jingwei = new employee(); This is to create an object, and we change it to jingwei = new employee("jingwei",20,'M',100,false); In this way, all the work is completed, haha! (Assign the "initial value" we want while creating the object)