Home > Java > javaTutorial > Detailed explanation of object-oriented content

Detailed explanation of object-oriented content

巴扎黑
Release: 2017-06-26 09:27:53
Original
1246 people have browsed it

Whether it is object-oriented or process-oriented, they are both ways of solving problems, but from different angles.

Process-oriented:

emphasizes every aspect of solving problems Every step is done personally, and every detail is implemented manually.


Object-oriented:

Use specific functional objects to solve specific problems, every detail No need to pay attention, just create the corresponding object.


Object-oriented is based on process-oriented

Classes and objects and their relationships
Class: a collective name for things with the same characteristics and behaviors (functions), an abstract concept
Object: a certain individual in this type of thing


Classes and objects Relationship
A class can create multiple objects. A class is the abstraction of an object, and an object is an instance of a class.

Describes a thing----> Class

Characteristics ----> Member attributes (member variables)
Behavior (function) ---> Member methods

Class creation format:
Modifier class 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: Identifier See the name to know the meaning
Member variable list: the characteristics of things, only write the characteristics that the current scene is concerned about, it is not necessary to write all the characteristics of the things. The writing method is similar to defining variables
Member method: the behavior (function) of things, only write the characteristics that the current scene is concerned about Behavior (feature) is written similarly to the previous definition method. The difference is that static

needs to be removed when writing at this time. When a class is used for the first time, it must be loaded into the method area, and it must be used every time thereafter. The class does not need to be loaded repeatedly

The difference between member variables and local variables
1. The definition location is different
Local variables: defined inside the method
Member variables : Defined inside the class, outside the method
2. The storage locations in the memory are different
Local variables: Local variables are stored in the stack area
Member variables: Member variables are stored in the heap area
3 .Different life cycles
Local variables: generated with the call of the method, disappear with the end of the method relatively short
Member variables: generated with the creation of the object, disappear with the recycling of the object data relatively short Long
4. Different initial values
Local variables: no initial value, must be assigned before use
Member variables: have initial value, reference type null, integer 0, decimal 0.0 characters: space, Boolean false

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

Class name: Person
Attribute: name ,age
Method: Demonstrate

Benefits of encapsulation: Improve program security

Steps to encapsulate attributes:
1 .Use the private keyword to modify the corresponding attribute to prevent the outside world from directly accessing the attribute through the object name. Attribute
2. Write the set and get methods of the corresponding attribute to give the outside world a channel to access the attribute

Encapsulation It is an object-oriented programming language's simulation of the objective world. In the objective world, member variables are hidden inside the object and cannot be directly manipulated or modified by the outside world.
Principles of encapsulation:
Hide content that does not need to be provided to the outside world.
Hide the properties and provide public methods to access them.
Member variables are private, providing corresponding getXxx()/setXxx() methods

Benefits:
Control the operation of member variables through methods, which improves the security of the code
Encapsulate the code with methods to improve the reusability of the code

Proximity principle of variable access:
When multiple variables with the same name appear, the target code uses the variable closest to this line of code

Problems that this can solve:
When local variables and member variables have the same name, and you want to access the member variables at a specific code point, you can add this in front of the variable name in the target line.
this only is the object that is currently calling this method
whoever calls this refers to who

Construction method
is a method. Function: assign a default value to the object's data

Definition format:
Modifier Method name (parameter list) {

Several initialization statements

}
Modifier: public
Method name: direct Write the class name
Parameter list: The same as writing the formal parameters of the previously defined method
Note that the constructor does not have a return value type

If we do not manually write a parameterless constructor, the system will provide one by default , if we manually write a constructor without parameters,
then the system will not provide it. When using it, we will directly use the constructor we wrote ourselves

The constructor is also a method and can be overloaded.

Whenever we write any constructor by hand, the system will not provide a parameterless constructor.
When we must use a parameterless constructor to create an object, we must write it by ourselves. Constructor without parameters

The above is the detailed content of Detailed explanation of object-oriented content. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template