Object-oriented (class, encapsulation, this, constructor), object-oriented this

Source: Internet
Author: User

Object-oriented (class, encapsulation, this, constructor), object-oriented this

Both object-oriented and process-oriented are the solutions to the problem, but they have different perspectives.

Process-oriented:

Emphasize that every step of solving the problem is done by yourself, and every detail is implemented manually.


Object-oriented:

You can use a specific feature object to solve specific problems. You only need to create the corresponding object for every detail.


Object-oriented is based on process-oriented

Class and object and Their Relationship
Class: a general term for things with the same features and behaviors (functions). It is an abstract concept.
Object: a definite individual in such a thing


Relationship between classes and objects
A class can create multiple objects. A class is an abstract object and an object is an instance of a class.

Describe a thing ----> class

Feature ----> member attributes (member variables)
Behavior (function) ---> member Method

Class creation format:
Modifier class name {

Data Type member variable name 1;
Data Type member variable name 2;
Data Type member variable name 3;
...

Member method 1;
Member method 2;
Member method 3;
...
}

Modifier: public
Class Name: The identifier is known
Member Variable list: the feature of a thing. It only describes the features of the current scenario. It is not necessary to write all the features of a thing. The syntax is similar to defining variables.
Member method: the behavior (function) of a thing only writes the behavior (feature) that is followed by the current scene, similar to the previous definition method. The difference is that if you write it at this time, you need to remove static

When a class is used for the first time, it needs to be loaded into the method area. You do not need to reload it every time you use this class.

Differences between member variables and local variables
1. Different Positions are defined.
Local variable: defined inside the Method
Member variable: defined inside the class and outside the Method
2. Different storage locations in the memory
Local variables: local variables are stored in the stack area.
Member variables: member variables are stored in the heap area.
3. different lifecycles
Local variable: this variable is generated when a method is called and disappears as the method ends.
Member variable: generated when an object is created and disappears after the object data is recycled.
4. Different initial values
Local variable: no initial value. A value must be assigned before use.
Member variable: has an initial value, the reference type is null, the integer is 0, the decimal number is 0.0 characters: space, Boolean false

Encapsulation
Encapsulation is one of the three characteristics of object-oriented, and the other two are inheritance polymorphism.

Class Name: Person
Property: name, age
Method: Display

Encapsulation benefits: Improve program Security

To encapsulate an attribute, follow these steps:
1. Use the private keyword to modify the corresponding attribute to prevent external users from directly accessing the attribute through the object name. attribute.
2. Write the set and get methods corresponding to the attribute to provide an external channel for accessing the attribute.

Encapsulation is a simulation of the objective world by object-oriented programming languages. member variables in the objective world are hidden inside the object and cannot be directly operated or modified by the outside world.
Encapsulation principles:
Hide all content that does not need to be provided externally.
Hide attributes and provide public methods to access them.
The member variable private provides the corresponding getXxx ()/setXxx () method.

Benefits:
Using methods to control member variable operations improves code security
Code is encapsulated using methods to improve code reuse.

Principle of proximity for variable access:
When multiple duplicate variables appear, the target code uses the variable closest to this line of code.

This can solve the following problems:
If you want to access the member variable in a specific code when the local variable and the member variable have the same name, you can add this before the variable name of the target row.
This is only the object currently calling this method.
Who calls this refers to WHO

Constructor
Is a method. function: Assign the default value to the object data.

Definition Format:
Modifier method name (parameter list ){

Several initialization statements

}
Modifier: public
Method Name: directly write the class name
Parameter List: Consistent with the previously defined method write Parameters
Note that the constructor has no return value type

If we do not manually write a constructor without parameters, the system will provide one by default. If we manually write a constructor without parameters,
Then the system will not provide it. We will directly use the self-written constructor when using it.

The constructor method can also be overloaded.

However, if we hand-write any constructor, the system will not provide a constructor without parameters,
When you need to create an object using a constructor without parameters, you must manually create a constructor without parameters.

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.