Class and encapsulation

Describe the introduction and use of the class

1. Relationship between classes and objects

A class is an abstraction of a class of things in real life that have common properties and behaviors. An object is a real entity that can be seen and touched.

2. Definition of class

Classes are the basic building blocks of Java programs. Class Composition: Properties: Represented in a class through member variables (variables outside of methods in the class). Behavior: This is represented in the class through member methods (remove the static keyword compared to the previous method). Class definition steps: Define the class; Write class member variables. Write the member methods of the class. Public class name {// member variable // member method… .}

3. Use of objects

Create the object. Object name = new class name ();

                    Phone   p   = new  Phone();

Use objects: (1) Use member variables. The variable name p.brand (2) uses the member method. Object name. Method name () p.Call ()

4. Member and local variables

Member variables A local variable The difference between
Outside of a method in a class Within a method or on a method declaration The position in the class is different
Heap memory Stack memory The location in memory is different
It exists as the object exists, and it disappears as the object disappears Exists as the method is called and disappears as the method is called Lifecycle difference
There is a default initial value It must be defined before it can be used Different initialization values

2. Enclosed

1. The private key

Is a permission modifier; Members can be decorated; A private member can only be accessed by the class in which it is used. (1) Provide a “get variable name ()” method to get the value of the member variable, and use a public method to modify the member variable. (2) Provide “set variable name (parameter)” method, used to set the value of the member variable, the method is modified with public.

2. This keyword

This represents the object reference of the class in which the method is called, and this represents the object in which the method is called. Variables decorated with this are used to refer to member variables: If a method parameter has the same name as a member variable, a variable that is not decorated with this refers to the parameter, not the member variable. If a method parameter does not have the same name as a member variable, a variable that is not decorated with this refers to a member variable.

3. Packaging

Encapsulation is one of the three characteristics of object-oriented programming language (encapsulation, inheritance, polymorphism). It is the simulation of the objective world by object-oriented programming language. The member variables of the objective world are hidden inside the object, and the outside world cannot operate directly. Encapsulation principle: some information of the class is hidden inside the class, does not allow external programs to directly access, but through the method provided by the class to realize the operation of the hidden information and access the member variable private, provide the corresponding get()/set() method. Encapsulation benefits: Methods are used to control the operation of member variables, increasing code security. Encapsulation of code with method improves the reusability of code.

4. Construction method

The construction method is a special method. Role: Create an object. Format:

Public class name {class name (parameter){}}

Function: It is mainly to complete the initialization of object data. Note: (1) Constructor creation: If no constructor is defined, the system will give a default no-parameter constructor. If a constructor is defined, the system no longer provides a default constructor. (2) Overloading of constructors: If you customize a constructor with parameters and also use a no-parameter constructor, you must write another no-parameter constructor. (3) Recommended Usage: Handwrite the parameterless constructor whether it is used or not.