Object-Oriented Programming, my thoughts (5)

Source: Internet
Author: User


2.3 explore functions in depth:

2.3.1 constructor, default constructor, and default constructor
For the above instance, it can already complete most of the work, but it is still not perfect, there are many details to be improved! Some people may have noticed that when I create the "jingwei" object, all the attributes of this object are empty, that is: the object name is uncertain, the age is uncertain, the gender is uncertain, the salary is uncertain, and the lunch is uncertain. If we want to add these attributes, we need to use the object to call the corresponding methods and modify them one by one! Oh, my God, this is too much trouble! Is there any good way to assign values to attributes while creating objects? Oh no. Should I say it is the initialization of the attribute? Of course, no problem. This requires the so-called constructor! Constructor is the most special function in the class. It is opposite to the function of the Destructor! In terms of features: 1. It is the only function in programming languages that has no return value type. 2. Its name and class name must be exactly the same. 3. It must be declared as public type 4 and can be overloaded. 5. It is automatically called when an object is created. Function: 1. It initializes the attributes in the class. In fact, for the above program, we do not have a self-defined constructor. However, in this case, the system will automatically define a "default constructor" for us ". It automatically assigns a value to 0 and a Boolean variable to false (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 function for your program. (Here, we advocate self-defining constructor rather than using the default constructor of the system.) Let's look at an instance! This is clear!
// Employee. java

Public class employee {

Private String name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private boolean lunch; // employee lunch

//...... And so on.

Public employee () {// This is the "default" constructor.

Name = "jw"; // set employee name

Age = 20; // set the employee's age

Sex = "M"; // sets the employee gender.

Emolument = 100; // set employee salaries

Lunch = false; // set employee lunch

}

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

//...... And so on.

}; In this way, when we create the "jingwei" object, all its attributes are also initialized! Obviously, this greatly improves the work efficiency, but it still does not meet the requirements. Think about what will happen when we create the second object of this type? It tells you that except for the object's "name" (this name is not the name in the object property, but the name of the object itself), all its "property values" are the same! For example, now we create the second object flashmagic, but I will find that all the attributes of this object are exactly the same as those of jingwei. However, we can only use the object method to change the write attribute! Obviously, this method is not good! We need a way to assign the desired value to the object property when creating the object ". As you can see, the default constructor is 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! Let's just look at an instance:
// Employee. java

Public class employee {

Private String name; // employee name

Private int age; // employee age

Private char sex; // employee gender

Private float emolument; // employee salary

Private boolean lunch; // employee lunch

//...... And so on.

Public employee (String n, int a, char s, float e, boolean l) {// check this constructor.

Name = n; // set employee name

Age = a; // set the employee's age

Sex = s; // set employee gender

Emolument = e; // set employee salaries

Lunch = l; // set employee lunch

}

Public void heater () {// This method is used to process employees' lunch

Lunch = true;

}

//...... And so on.

}; In this way, when creating an object, we can give it the value we want. Obviously, this is much more convenient. Oh, right! I haven't told you how to create it yet! Haha, you will see this sentence a few pages forward: 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 has been completed! (When creating an object, the desired "Initial Value" is assigned ")

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.