Class & Object

#Java is based on object-oriented, focusing on objects, splitting one thing into different objects, and relying on the interaction between objects.

process oriented 

The focus is on the process, and the whole process involves behavior, which is function.

Object Oriented Concepts

The focus is on the object, that is, the main body involved in the participation process, which links each function realization through logic.

1. Object-oriented is a way of thinking about problems, a kind of thinking.

2. A class is a general term for a class of objects, and an object is a specific instance of this class.

3. The benefits of object-oriented: make complex things simple, as long as you face one object.

Classes and class instantiations

A class is equivalent to spoofing a template. An object is a sample generated by a template. A class can generate countless objects.

basic grammar

Example:

class is the keyword for defining the class, Person is the name of the class, and {} is the body of the class.

Elements in a class are called member properties. Functions in a class are member methods.

person is a variable, not an address, but this variable stores an address, so this variable is also called a reference.

instantiation of the class

The process of creating an object with a class type is called instantiation

1. A class is just a model, which defines the members of the class.

2. A class can instantiate multiple objects. The instantiated objects occupy actual physical space and store class member variables.

3. The variable defined by static is a class variable, which belongs to the class and cannot be defined in the method.

4. Static methods can be called in ordinary methods, but ordinary methods cannot be called in static methods, because static methods do not depend on objects, and ordinary methods depend on objects, so static methods can be called. Ordinary methods cannot define static variables.

static keyword

a. Modified attributes

b. Modification method

#Static methods belong to classes, not objects

#You can call static methods directly without creating an instance of the class.

#Static methods can access static data members, and can change the value of static data members.

#Static methods have nothing to do with instances, but with classes. The main method is a static method.

c. Code block

d. Modified class

Encapsulation (private)

Benefits of encapsulation: more secure.

The two keywords private/public represent "access control".

A member variable or member method modified by public can be used directly by the caller of the class.

A member variable or member method modified by private cannot be used by the caller of the class.

Private can not only modify fields, but also methods.

Construction method

A class has at least one constructor. If no constructor is implemented, the compiler will generate a constructor without parameters for us by default.

 If the current class has other constructors, the compiler will not help us generate a constructor without parameters.

Constructors can be overloaded before.

this keyword

1.this.data calls the properties of the current object.

2.this.func() calls the method of the current object.

3.this() calls other constructors of the current object.

code block

native code block

Example code block 

Static code blocks (executed first, and only executed once, can be executed without instantiation.)

Synchronized code block

Anonymous object (used only once)

Guess you like

Origin blog.csdn.net/m0_62764440/article/details/121442250