On java object-oriented (class, encapsulation, this, construction method)

Whether object - oriented or process - oriented, these two are just ideas to solve problems, but from different angles

Process oriented:

It is emphasized that every step of solving the problem should be done by yourself, and every detail should be implemented manually

object-oriented:

Use specific function objects to solve specific problems. You don't need to pay attention to every detail, just create corresponding objects

Object oriented is based on process oriented

Classes and objects and their relationships

Class: the general name of things with the same characteristics and behavior (function). It is an abstract conceptual object: a certain individual in this kind of things

Relationship between classes and objects

A class can create multiple objects. A class is an abstraction of an object and an object is an instance of a class

Describe a thing - > class

Feature - > member attribute (member variable) 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: see the meaning of the identifier

Member variable list: the characteristics of things. It only writes the characteristics of the current scene. It is not necessary to write all the characteristics of things Writing is similar to defining variables

Member method: the behavior (function) of things is only written for the behavior (feature) concerned in the current scene. The writing method is similar to the previous definition method. The difference is that static should be removed when writing at this time

When a class is used for the first time, it should be loaded into the method area. There is no need to load it again every time the class is used again

Differences between member variables and local variables

1. Different defined positions

Local variable: defined inside the method. Member variable: defined inside the class and outside the method

2. Different storage locations in memory

Local variables: local variables are stored in the stack area, and member variables: member variables are stored in the heap area

3. Different life cycles

Local variable: it is generated with the call of the method and disappears with the end of the method. Relatively short member variable: it is generated with the creation of the object and disappears with the recovery of the object data

4. Different initial values

Local variable: it has no initial value and must be assigned before use. Member variable: it has initial value, reference type null, integer 0, decimal 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 attribute: name, age method: display

Encapsulation benefits: improved program security

To encapsulate attributes:

1. Use the private keyword to modify the corresponding attribute to prevent the outside world from passing through the object name Property to access the property directly

2. Write the set and get methods of the corresponding attributes to give the outside world a channel to access the attributes

Encapsulation is the simulation of the objective world by object-oriented programming language. In the objective world, the member variables are hidden in the object, and the outside world can't operate and modify them directly.

Packaging principles:

Hide all contents that do not need to be provided externally.

Hide properties and provide public methods to access them.

The member variable private provides the corresponding getxxx() / setxxx() methods

Benefits:

The method is used to control the operation of member variables, which improves the security of the code

The code is encapsulated by method, which improves the reusability of the code

Proximity principle of variable access:

When multiple variables with duplicate names appear, the object code uses the variable closest to this line of code

Problems this can solve:

When the local variable and the member variable have the same name, if you want to access the member variable in a specific code, you can add this before the variable name in the target line

This is only the object that currently calls this method.

Who calls this means who

Construction method

This is a method Function: assign a default value to the data of the object

Define format:

Modifier method name (parameter list){

Several initialization statements

}

Modifier: public

Method name: write the class name directly

Parameter list: consistent with the method write form parameters defined earlier

Note that the constructor has no return value type

If we don't manually write a parameterless constructor, the system will provide one by default. If we manually write a parameterless constructor,

Then the system will not be provided. When using it, we will directly use the construction method written by ourselves

Construction methods are also methods, and can also be overloaded

If we write arbitrary construction methods, the system will not provide parameterless construction methods,

When you have to create an object with a parameterless constructor, you must write the parameterless constructor yourself

The above article on java object-oriented (class, encapsulation, this, construction method) is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>