Home  >  Article  >  Java  >  Java basic introductory essay (7) JavaSE version - object-oriented definition, features: encapsulation, constructor

Java basic introductory essay (7) JavaSE version - object-oriented definition, features: encapsulation, constructor

黄舟
黄舟Original
2016-12-22 13:06:261450browse

Object-oriented

Process-oriented: For process-oriented thinking, the emphasis is on process (action).

Object-oriented: For object-oriented thinking, the emphasis is on objects (entities).

Features:

1. Object-oriented is a common idea. In line with people's thinking habits.
2. The emergence of object-oriented simplifies complex problems.
3. The emergence of object-oriented technology has turned the executors who were once part of the process into the commanders of the objects.

Classes and objects:

java language describes things in real life and is reflected in the form of classes.

We usually only focus on two aspects when describing things: one is attributes and the other is behavior.

Just clarify the properties and behavior of the thing and define it in the class.

Category: Description of things.
Object: an instance of this type of thing. Created through new in java.

Defending a class is actually defining the members in the class.
Members: member variables<-->properties, member functions<-->behaviors.

The difference between member variables and local variables:

1. Member variables are defined in the class and can be accessed throughout the class.
Local variables are defined in functions, statements, and local code blocks, and are only valid in the area they belong to.

2. Member variables exist in objects in heap memory.
Local variables exist in methods in stack memory.

3. Member variables exist when the object is created and disappear when the object disappears.
Local variables exist with the execution of the area to which they belong, and are released with the end of the area to which they belong.

4. Member variables have default initialization values.
Local variables have no default initialization value.

Anonymous object

Anonymous object: an object without a name, such as new Car(); is actually the abbreviation format of the defined object.
Points to note when using anonymous objects:

1. When an object only calls a method once, it can be simplified into an anonymous object.

2. Anonymous objects can be passed as actual parameters. For example, show(new Car());

Object-oriented feature one: Encapsulation

Encapsulation: refers to hiding the properties and implementation details of the object and only providing public access to the outside world.

Benefits:

Isolate changes.

Easy to use.

Improve reusability.

                              Improved security.

Principle of encapsulation:

                                                                                                                                                                                                        ourselves, will hide the content that does not need to be provided to the outside world.

                       Hide all properties and provide public methods to access them.

private: Private, is a permission modifier, used for member variables, not for local variables.

                                                                               Private content is only valid in this category.

Note: Private is just a manifestation of encapsulation, and encapsulation can be completed without being private.

Constructor

Constructor: A function called when constructing an object. Function: It can initialize the object.

Note: If there is no constructor defined in a class, then there will be a default empty parameter constructor in the class; if a specified constructor is defined in the class, then there will be no default constructor in the class.

The difference between general functions and constructors:

Constructor: When an object is created, the corresponding constructor will be called to initialize the object. It will be called and only called once.

General function: After the object is created, it is called only when the function is needed. It can be called multiple times.

Constructor overloading

When do you define a constructor?

When describing a thing, there are some contents that the thing has as soon as it exists. These contents are defined in the constructor.

Features:

1. The function name is the same as the class name.

2. There is no need to define the return value type.

3. There is no specific return value.

Function:

Initialize the object.

Note:

1. Characteristics of the default constructor.

2. Multiple constructors exist in the form of overloading. (When overloading, please note that the function names are consistent, and the parameter types at the corresponding positions are different and overloaded)

Details that need to be paid attention to in the constructor:

1. The constructor is only called once during initialization, and the general function can be repeated multiple times transfer.

2. The constructor can contain general functions, but general functions cannot directly call the constructor. They can only be called when an object is created in a general function.

3. The constructor has no return type and is generally named in uppercase letters consistent with the class name.

4. Create objects can only be created in the existing constructor format. The default constructor refers to the case where no constructor is defined.

5. The constructor can contain return, which is the end of the function and is generally not needed.

The above is the content of Java Basic Introduction Essay (7) JavaSE version. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
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