Thinking in Java study notes

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_43442524/article/details/100077132

Introduction to Object

Abstraction

All programming languages ​​provide abstractions. All is the target program is a collection of objects, they are told to do by sending messages to each other. All objects each object has its own type which has a particular type of each object is stored by the other objects made of the same message can accept objects having a state, behavior and identity

Each object has an interface

How can we get useful objects? There must be some way generated request for an object, the object is to accomplish various tasks, such as completing a transaction, to draw on the screen, open switch. Each object can only meet some of the requests, which have the object interface (interface) defined in the decision is the type of interface. Light bulb, for example to do a simple analogy:

Light lt = new Litght();
lt.on();

The interface is determined for a particular object request can be issued.

In the above example, the name of the type / class is Light, Light noun specific object is It, the request may be issued to an object Light: open it, close it, it will be brighter, it will dim. You create a Light in the following manner objects: the definition of the object 'reference' (It), and then call the new method to create a new object of that type. To send a message to an object, the object needs to declare the name of a dot and a symbolic link request message.

Each object is to provide services

The program itself will provide users with the service, he will aim to achieve this by calling other services provided by the object.

Hidden concrete realization

java with the three keywords set boundaries, public, private, protected java there is a default access within the class, when not using any access to specified terms mentioned above, it will play a role. Such permission is usually called package access

Multiplexing realization

Code reuse is one of the greatest advantages of object-oriented programming languages ​​are provided.

inherit

When you inherit an existing type, it created a new type. This new type not only include all members of the existing type (although private members were hidden and inaccessible), but more importantly, it replicates the base class interface. That is, all messages may be sent to the base class is also subject to a derived class objects can be sent. Derived class as the base class of the same type.

A is a circular geometry.

There are two ways to make the base class and derived class variance. Add a new method directly in the derived class, the new method is not part of the base class interface. Coating the base class method of changing the existing coverage.

Along with interchangeable multi-state objects

The most important twists that make object-oriented programming: the compiler can not generate a function call in the traditional sense.

If you use Java to write a method (later you will soon learn how to write):

void doSomething(Shape shape){
    shape.erase();
    shape.draw();
}

This method can dialogue with any Shape, so it is independent of any other type of it to draw and erase objects.

If the program is used in other parts of the doSomething () method:

Circle CirCie,new Circle();
Triangle triangle = new triangle();
Line line = new Line():
doSomething(circle);
doSomething(triangle);
doSomething(line);

Call to doSomething () will move to the correct handling of white, but regardless of the exact type of object.

This is a pretty amazing trick. Consider the following line of code:

doSomething(circle);

When Circle is passed to the intended recipient Shape method, exactly what will happen. Since the Circle can be doSomething () seen as Shape, that is to say, doSomething () messages can be sent to any of Shape, Circle can receive, then, to do so is completely safe and logical.

Single inheritance structure

In OOP, since c ++ has become available on a very eye-catching question is whether all classes ultimately inherit from a single base class. In java (in fact also includes all OOP languages other than C ++ '), the answer is yes, the name of this ultimate base class is Object . Facts have proved that a single inheritance structure brings many benefits.

All objects in a single inheritance structure have a common interface, so that they are ultimately the same basic type.

container

This is commonly called a container (also known as a collection of Java class libraries but in a different sense, "collection" is a term, so this book using the "container" is the word) of new objects, can expand their own when needed to any you put everything to accommodate them. Therefore, the future will not need to know how many objects are placed in a container, you only need to create a container object, and then let it handle all the details.

Creating and lifetime of the object

When using objects, one of the most critical issues is the generation and destruction of their way. Each object requires resources in order to survive, especially memory. When we no longer need an object, it must be cleaned up so that it occupies resources are freed and reused.

Java uses a fully dynamic memory allocation. Whenever you want to create a new object, it is necessary to build a dynamic instance of this object using the new keyword.

Java provides garbage collection mechanism, which automatically discovers when an object is no longer in use, and then destroy it.

Exception Handling: Handling errors

Exception handling error handling directly into the programming language, sometimes even placed in the operating system. Exception is an object, which is "thrown", and is designed to handle a particular type of error exception handler corresponding to "error from the capture location." Exception handling is like a path parallel with the normal execution of the program, in another path executing when the error occurred because it is another completely separate execution path, so it does not interfere with normal code execution. this often makes the code easier to write, because not forced to regularly check for errors.

In addition, error value, and a method for exception is not thrown returned provided to indicate error conditions such as bit flags may be ignored. An exception can not be ignored, so it will be processed to ensure that somewhere.

Finally, it should be pointed out that: abnormal provides a way to reliably recover from error conditions. Now is no longer the only exit the program, you can often be corrected, and resume execution of the program, have helped write more robust programs. Java exception handling particularly noticeable in many programming languages, Java since the beginning built-in exception handling, and forces you to have to use it.

Concurrent Programming

There is a fundamental concept in computer programming. That is, multiple tasks at the same time thought processes. Many programming problems require that the program be able to stop the work being done and instead deal with some other issues. And then returns to the main process.

Sometimes interrupt handling for time-sensitive tasks are necessary, but for a lot of other issues, we just want the problem into multiple portions (tasks) can be run independently, thereby improving the responsiveness of the program. In the process, these portions operate independently from one another thread called the concept is known as "concurrent."

to sum up

You know the procedural language what a word looks like: data definitions and function calls. Want to know the meaning of such a program, you must be busy for a while, you need to read through the function calls and low-level concepts to create a model in mind. This is what we in the process of programming, the need for intermediate representation of reasons. These programs always easy to confuse people, because they use the term represents more computer-oriented rather than problem you want to solve.

Guess you like

Origin blog.csdn.net/qq_43442524/article/details/100077132
Recommended