Explain the object-oriented programming idea of Java in an all-round way

What is object oriented? Object object oriented programming

Object oriented is programming with objects, which is abbreviated as OOP.

SP vs OOP

Three principles of object-oriented encapsulation inheritance polymorphism

object

Object is the core part of object-oriented programming. It is a concrete entity with clearly defined state and behavior; In fact, an object is the encapsulation of "data" and "function", in which: data represents its own state, also known as "attribute" or "member data"; Functions represent their own functions, also known as "methods" or "member functions".

In order to better understand the world, people divide things (objects) in real life into classes; things in the same class always have some commonalities; classes define entities with common characteristics and behaviors; classes are a set of objects with the same attributes and behaviors.

The characteristics of attribute things are represented by variables in classes; Each attribute of each object has its specific value; The attribute name is shared by all objects of the class; Features owned by an object or entity are called attributes when they are represented in a class

Methods the behavior and action of things are represented by functions in classes; Each object has the same action and behavior; Object is represented in the class as a method.

The difference between class and object class is the "template" or "prototype" used to describe an entity; Objects are actual entities, and each object is a concrete instance of a class; Class is used to define all attributes and methods of an object. All objects of the same class have the same characteristics and operations; A class can be understood as a mold for producing products, and an object is a product produced according to this mold.

Class and structure

To pack something together and present it in a new and complete form; The processing method of hiding attributes, methods or implementation details is called encapsulation; Encapsulation is to selectively disclose or hide some information, which solves the problem of data security.

Inheritance is a feature of reusing existing classes to generate new classes; Generally speaking, it is the process of creating a new class (subclass or derived class) from an existing class (i.e. parent class or base class); in real life, inheritance can achieve the purpose of property reuse, while in Java, inheritance can reuse code.

Polymorphism means that the same function has different implementations in different classes; The advantage of polymorphism is to make classes more flexible and easy to expand.

There is another "abstraction" that has to be said here. The process of classifying the same or similar objects is abstraction. Therefore, abstraction is the method of analyzing problems; The basic principle of abstraction: only care about the main problems, not the secondary problems; Only care about the main contradiction, not the secondary contradiction; Only care about the same things, not different things; Only care about what the problem is and what can be accomplished, not how to accomplish it. Abstract process is actually the core idea of object-oriented programming.

Defining classes in Java

Creating objects in Java

The syntax of creating an object is similar to that of an array. The object is also a reference data type. You can only use the new operator to allocate memory from the heap; General syntax for creating objects: class name reference name = new class name (); Using a defined class, the process of creating such an object is called instantiation.

Member operator '.' Members (properties and methods) in the class can only be accessed by instantiating the object of the class first; use the member operator (.) to access member properties or member methods; the general syntax is: object name. Member name, such as:

Access rights: members of structures in public and private c languages can access from anywhere, which will leave a great hidden danger to the security of data; In order to avoid data corruption caused by directly accessing class members from outside the class, Java has formulated constraints on the access of class members; The keywords public and private are access modifiers to indicate whether a member can be accessed from outside the class; Members decorated with public can access anywhere without any constraints; Members decorated with private can only be accessed by other members of the class, not from outside the class.

Private members cannot be accessed from outside the class; Private members of other classes are also hidden from the current class.

Access rights example

Access rights (Continued) sometimes, adding an access modifier may bring inconvenience to the operation of data, but it can ensure the security of data to a great extent; generally, we will declare the member attribute as private and the member method as public, but this is not absolute; sometimes, some private data members may be operated outside the class, so we can add one Public method, and then use this method to operate private data to avoid data damage caused by misoperation outside the class; Because the main method is called by a virtual machine outside the class, the main method must be declared public. For example, modify the student class

Object initialization in the above example, you can only assign values to data members one by one. If you want to initialize member properties while instantiating an object, you use the construction method; The constructor is a special member method, which has the same name as the class and is automatically called by the virtual machine when the object is instantiated; Please note: the constructor has no return value type and cannot have a return value. Example of construction method:

Construction methods are generally used to allocate resources to data members or initialize data members because they will automatically call construction methods when instantiating objects; The general form of construction method: access permission class name (formal parameter list) {method body} because the construction method is called by the virtual machine, the construction method should generally be defined as public.

For example, add a constructor for the student class

Construction method (Continued) each object must execute the construction method during generation, and can only execute it once; if the construction method call fails, the object cannot be created; the construction method cannot be explicitly called directly; if the construction method is not defined, the class will automatically generate a default construction method without parameters, and the default construction method will do nothing; Once the construction method is explicitly defined, the default construction method disappears automatically. Therefore, two construction methods are generally defined: no parameter and parameter.

summary

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
分享
二维码
< <上一篇
下一篇>>