Home  >  Article  >  Java  >  How to understand java object-oriented

How to understand java object-oriented

silencement
silencementOriginal
2019-05-29 14:50:284422browse

How to understand java object-oriented

How to understand Java object-oriented?

Java is an object-oriented programming language. Accurately understand the orientation of Java. Only with objects can you master JAVA programming ideas and methods better and faster. This article introduces Java object-oriented to you to help you better understand Java.

1. What is an object?

First of all, let me explain that the object is not Java. In fact, it is just a concept and a program used to solve problems. Design thinking methods. To understand objects, you must combine them with object-oriented. In object-oriented, a class is formed after abstracting the common attributes and behaviors of a certain type of things. A practical example of a class is called an object. So it can be seen from the above that there is such a relationship between classes and objects: a class is the abstraction of a group of objects with common attribute names and behaviors, and an object is a real example of a class.

A class is the reflection of entities in the real world or the thinking world in the computer. It encapsulates data and operations on these data.

The class describes a collection of objects with the same characteristics (data elements, state variables) and behavior (operations on data elements, state variables). If I need a tool with certain functions, then I can customize this class to contain the data I need, as well as the functions provided by this class.

Objects are variables with class type. Classes and objects are basic concepts in object-oriented programming technology. A class is an abstraction of an object, and an object is a concrete instance of a class. Classes are abstract and do not occupy memory, while objects are concrete and occupy storage space. A class is a blueprint for creating objects, a software template that defines the methods and variables included in an object of a specific type.

JAVA is an object-oriented programming language. Objects are abstracted from classes. All problems are handled through objects. Objects can operate the properties and methods of classes to solve problems. Therefore, it is very necessary to understand the creation, operation and death of objects. An object can be thought of as abstracting a special case from a class of things. This special case is used to deal with problems that arise in this type of thing. In the Java language, objects are created through the new operator.

2. Objects and object references

The following expression: A a1 = new A; It means that A is a class, a1 is a reference, and a1 is not an object , new A is the object, and the a1 reference points to the new A object.

In JAVA, "=" cannot be regarded as an assignment statement. It is not assigning one object to another object. Its execution process essentially passes the address of the object on the right to the left The reference makes the reference on the left point to the object on the right. JAVA does not appear to have pointers on the surface, but its reference is essentially a pointer. What is stored in the reference is not the object, but the address of the object, making the reference point to the object. In JAVA, the "=" statement should not be translated into an assignment statement, because what it performs is indeed not an assignment process, but a process of transmitting an address. Being translated into an assignment statement will cause many misunderstandings and the translation is inaccurate. .

Another example: A a2; It represents that A is a class, a2 is a reference, a2 is not an object, and the object pointed by a2 is empty null;

Another example: a2 = a1; It represents , a2 is a reference, a1 is also a reference, the address of the object pointed by a1 is passed to a2 (address transfer), so that a2 and a1 point to the same object.

To sum up, it can be simply recorded as, during initialization, the left side of the "=" statement is a reference, and the new one on the right side is the object.

When the following "=" statements are referenced on the left and right, the left and right references simultaneously point to the object pointed to by the right reference. The so-called instance is actually a synonym for object.

3. Java Inheritance Analysis

Inheritance is a significant feature of object-oriented. Inheritance is the derivation of a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can expand new capabilities. Java inheritance is a technology that uses the definition of an existing class as a basis to create a new class. The definition of a new class can add new data or new functions, or use the functions of the parent class, but it cannot selectively inherit the parent class. This technology makes it very easy to reuse previous code, which can greatly shorten the development cycle and reduce development costs.

Inheritance is a cornerstone of Java object-oriented programming technology because it allows the creation of hierarchical classes. Inheritance can be understood as the process by which one object obtains properties from another object. If class A is the parent class of class B, and class B is the parent class of class C, we also say that C is a subclass of A, and class C inherits from class A. In Java, class inheritance is single inheritance, that is, a subclass can only have one parent class. The two keywords often used in inheritance are extends (IS-A, what is it) and implements (Has-A, what function does it have). The use of these two keywords determines whether one object has an IS-A relationship with another object.



The above is the detailed content of How to understand java object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn