Four, class and object

Class and object

The basic characteristics of object-oriented

  • Encapsulation-refers to the method of packaging or hiding the data and the operation method of the data
  • Inheritance-the essence is the abstraction of a certain batch of classes (another benefit of inheritance is code reuse)
  • Polymorphism-Polymorphism does not decide which operation to implement according to the actual situation until the system is running

Object

The object in the program comes from life, and an entity in real life becomes an object in the program after being abstracted and modeled, that is, an object described by a specific symbol

Objects have their own characteristics, we call attributes (static characteristics) and methods (dynamic characteristics)

Such as: students, attributes are determined by student ID, name, gender, etc., and methods include study, course selection, etc.

class

In programming, the common characteristics of a group of similar objects are abstracted and stored together to form a class

to sum up

  • Object: a specific thing, we call it object, or instance (instance)
  • Class: an abstraction of objects, we call it class
  • The relationship between objects and classes: special to general, concrete to abstract
  • A class can be seen as a template for a class of objects, and an object can be seen as a specific instance of the class
  • A class is an abstract concept used to describe objects of the same type. The class defines the static and dynamic properties that this type of object should have
  • Objects are the core of a Java program. In a Java program, "everything can be an object"

Class definition

Class is the basic element of Java, it is the template of the object, all the objects in the Java program are created by the class

A Java class mainly consists of two parts: the declaration of the class and the body of the class

Class declaration

​ Format: [modifier] class <class name> [extends parent class name] [implements interface list] {}

Body of the class

​ It is mainly composed of two parts: the definition of member variables and the definition of member methods

Class member method definition

​ The behavior of the class in Java is implemented by the member method of the class

​ The member method of the class consists of two parts: the method declaration and the method body

Format: [modifier] <type returned by the method> <method name> ([method list]) {method body}

Object creation

Class is a template for creating objects

Class name object name = null; //declare object

Object name = new class name (); //Instantiate the object

The two steps can also be combined into one line:
class name object name = new class name ();

Construction method

The constructor is used to construct an instance (object) of the class. Java calls the constructor through the new keyword to return an instance of the class

Construction method is a special method

Format: [modifier] class name (parameter list) {…}

Key points:

  • The constructor method name must be the same as the class name
  • Called by the new keyword
  • Cannot call return in the constructor
  • If no constructor is defined, the system will automatically define a constructor without parameters

Overload of the construction method (overload)

​ Method overloading means that multiple methods with the same name but different parameters can be defined in a class. When calling, the corresponding method will be selected according to different parameter lists.

  • When the method is called, match the closest type
  • The meaning of different parameter lists are: type, number, and order are different
  • Only the return value is different, and does not constitute an overload of the method
  • Only the names of the formal parameters are different, which does not constitute an overload of the method

static

  • If you put static before a member variable declaration, the variable becomes a static variable, also called a member variable of the class
  • If you put static before the definition of a member method, the method is a member static method, also called a member method of the class

You can use class name, class attribute or class name. Method name to directly call static variable or static method

note

  • Static variables belong to the class, and static variables are directly referenced by the class name
  • Non-static variables are called instance variables, and only exist after the instantiated object is generated
  • this cannot be used in static methods

Encapsulation

​ The similarities and differences between the four different permissions

Same class Same package Subcategories of different packages Non-subclasses of different packages
private
default
protected
public

Garbage collection mechanism

Java provides a method called finalize(), which is used to perform some resource recovery work before the object is destroyed by the garbage collection mechanism.

finalize() does not have any parameters and return values, each class has and only one finalize() method

The programmer can call System.gc(), "it is recommended that the garbage collection system reclaim resources", but when the garbage collection system is executed and whether it is executed, the programmer cannot control

note

  • Object space allocation: use the new keyword to create an object
  • Release of object space: Just assign the object to null, and the garbage collector will be responsible for reclaiming the memory space of all "unreachable" objects

Guess you like

Origin blog.csdn.net/weixin_42248871/article/details/115323468