Home > Java > javaTutorial > Detailed introduction to Java object introduction

Detailed introduction to Java object introduction

黄舟
Release: 2017-03-01 11:13:11
Original
1153 people have browsed it

(1) The process of abstraction of things

The complexity of the problems that people can solve depends on the ability to abstract things To what extent.

#Advanced mathematics and physics are highly abstract things. They are studied at that extremely abstract level, and once they make big progress, they will have a great impact on our daily lives. For example, Einstein's theory of relativity, the discovery of which had a huge impact on the world. The same is true for programming. We abstract some daily things. The higher the level of abstraction, the more concise code we can use to describe it.

Alan Kay once summarized the five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages ​​​​based on Java. These characteristics represent a pure object-oriented design approach. :

  1. Everything is an object

  2. A program is a collection of objects, and they tell each other what to do by sending messages

  3. Every object has its own storage composed of other objects

  4. Every object has its type

  5. All objects of a specific type can receive the same message

Or we can describe the object more concisely:Objects have state , behavior and identification . Each object has internal data, methods and a unique address.

(2) Each object has its own function

All objects are unique, but have the same characteristics and behaviors. part of the class. For example, there are many sparrows in the sky, and each sparrow is an independent individual, but these individuals are all sparrows, and they all have the characteristics of sparrows.

Each object is a service provider. We can perform our own operations by calling methods in this object and using variables in the object. Thinking of objects as service providers has a side benefit: it helps make objects more cohesive. High cohesion is one of the basic quality requirements of software design, which means that a software structure is well organized. In this class, all methods and attributes are born for one function. These methods and Properties are grouped together and are very convenient to use. But one of the problems we face when designing objects every day is that we often stuff too many functions into one object.
In good object-oriented design, each object can complete one task well, and they are not trying to do more. Such a design will have another benefit: Low coupling. Because each object has its own highly cohesive attribute methods, they are all born to do their own "things". Multiple objects of different types achieve this low coupling by combining their respective functions. Effect.

(3) Encapsulation - hidden specific implementation

In the seven-layer model of the computer network structure, each layer is closed to each other, and finally completed The entire network structure from the most virtual computer operations to the underlying electronic circuit operations. Between its layers, the upper layer can only see the interface of the next layer's method. It can only know how to call the next layer's method, but is not interested in the specific code implementation of the next layer.
In the java code, three keywords are used to set boundaries in the class. These three access designation words determine who can use the content that follows:

  1. public: Current class Current package Descendants classes Other packages

  2. protected: Current class Current package Descendants classes

  3. Default (Unmodified): Current class Current package

  4. private: Current class

(4) Reuse and combination

#The reuse and combination of code is one of the most amazing advantages provided by object-oriented programming languages.

The simplest way to reuse is to directly use an object of this class. In addition, you can also put an object of that class into a new class. We call this reuse method "creating a member object". The new class can be composed of any number of other objects of any type in any way that achieves the desired functionality in the new class. Because you are using existing classes to synthesize new classes, this concept is called combination.
We usually regard combination as having a relationship - "has-a".

Compared with many novices who like to use inheritance relationships in their own classes, in fact, when establishing a new class, you should first consider combination because it is more flexible. If you use this combination, the design will become clearer.

(5) Inheritance

We will not introduce what inheritance means. It is worth mentioning that when we consider how to abstract the parent class in the project, we can think about it Multiple subclasses, use multiple subclasses to find out what they have in common, which is very helpful for us to abstract the parent class.

Because of the inheritance relationship, all messages that can be sent to base class objects can also be sent to subclass objects. This means that, in effect, the subclass has the same type as the parent class . If we just inherit the parent class and only override the methods without creating new methods in the parent class, we can call it pure substitution. For pure substitution, we use - "is-a" to express this relationship. For those that not only inherit the parent class, but also add new methods based on the parent class, we use - "is-like-a" to express this relationship.

(6) Characteristics of polymorphism and late binding

When dealing with hierarchical structures, we often treat an object not as a specific object. Instead, they are treated as their parent class. For example, we need to pass in a list. At this time, we do not need to distinguish whether it is an ArrayList or a LinkedList in the parameters of the method. We only need to write the List. This polymorphism greatly enriches the program.

But when it comes to this, there is the simplest but difficult to explain problem: for example, the above method calls the add function after passing in a list. How does this method know which add function is called by the passed in List? ?

The answer to this question is also the most important tip of object-oriented programming: The compiler does not generate early-bound function calls in the traditional sense, but uses the late-binding method. A function call generated by a non-object-oriented compiler will cause so-called early binding. This binding method means that the compiler will generate a call with a specific function name, and the runtime will Resolves to the absolute address of the code to be executed. In order to solve this problem, OOP uses the concept of late binding. When sending a message to an object, the code being called cannot be determined until runtime. The compiler ensures that the called method exists and checks parameter return values, but it does not know the exact code that will be executed.

In C++, you must explicitly declare through the keyword virtual that you want a method to have the flexibility brought by late binding attributes, which means that it is not late binding by default. In java dynamic binding is the default behavior.

(7) Single root inheritance structure

In Java, all classes ultimately inherit from a single base class, and this base class is Object. It turns out that the single-root inheritance structure brings many benefits. Single-root inheritance makes implementing a garbage collector much easier. Since all objects are guaranteed to have their type information, there is no deadlock due to the inability to determine the type of an object.

(8) Object creation and life cycle

When talking about the creation and life cycle of objects, many languages ​​can’t help but come to mind. Its handling varies. C++ believes that efficiency control is the most important issue, so C++ gives programmers the right to choose. You can create new by yourself, but you must delete the space created by new, otherwise you will fall into the famous memory leak problem.
In Java, all objects are dynamically created in the heap memory pool. In this method, you don’t know how many objects are needed, what their life cycle is, and what their specific types are until runtime. Such problems can only be determined at the moment when the relevant code is executed while the program is running. So it takes a lot of time to allocate storage space in the heap, which may be much longer than the time to create storage space in the stack. Creating and releasing storage space on the stack typically requires only a single instruction. The time it takes to create heap storage depends on the design of the storage mechanism.

(9) Exception handling

Exception is an object. He was "thrown" from the point of error. and are "caught" by corresponding exception handlers specifically designed to handle specific types of errors. Exception handling is like another path that is executed in parallel with the normal execution path of the program when an error occurs. Because it is a completely separate execution path, it does not interfere with the normal execution of the code.

It is worth noting: Exception handling is not an object-oriented feature - although exception handling is often represented as an object in object-oriented languages. Exception handling existed before the advent of object-oriented languages.

The above is the detailed introduction to Java Objects. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template