Thinking in Java Note 1 object Introduction

Source: Internet
Author: User

1.1 abstract process

All programming languages provide abstract mechanisms. The complexity of the problems that people can solve directly depends on the abstract TypeAnd quality. 1. Establish the association between the machine model and the actual model to be resolved: laborious and difficult to compile 2. Model for the problem to be resolved: it is a good solution for a specific problem, but it is too limited. 3. object-oriented method: a program can apply to a specific problem by adding new types of objects. When reading the code that describes the solution, you are also reading the Problem description. Basic Features of object-oriented 10 thousand objects are all objects. 2. A program is a collection of objects, and objects communicate by sending messages. 3. Each object can contain other objects to build a complex system. 4. Each object has its own type. 5. All objects of a specific type can receive the same message. 1.2 each object has an Interface You can create a certain type of variables and then operate on these variables. Members or elements of each category share a certain commonality: each account has a balance amount, and each member has its own status: each account has a different balance amount. Each object belongs to a specific class that defines features and behaviors. Because a class describes a set of objects with the same characteristics and behaviors, programmers define classes to adapt to the problem, instead of being forced to use existing data types to represent the storage units in the machine. You can add new data types to extend the programming language as needed. The programming system accepts new classes and takes care of them and performs type checks like the built-in types. One of the challenges of object-oriented is to create a one-to-one ing between the elements in the Problem Space and the objects in the solution space. How to obtain useful objects? There must be some way to generate a request to the object so that the object can complete various tasks, such as completing a transaction. Each object can only meet certain requests, which are defined by the interface of the object. The interface determines the requests that can be sent to a specific object. The code in the program must satisfy these requests. These codes constitute the implementation together with the hidden data. 1.3 provide services for each object View objects as service providers: the goal is to create a series of objects that can provide ideal services to solve problems. It helps to provide object cohesion. Every object can complete a task well, but does not try to do more. 1.4 specific implementation of hiding Program developers are divided into class creators (creating new data types) and client programmers (using new data types ). The goal of the class creator is to build a class. These classes only expose the necessary parts to the client programmer, while hiding the other parts. The advantage is that some of the hidden clients cannot be accessed, and the creator can modify the hidden parts as needed. 1.5 reuse implementationCombine and inherit. Avoid overly complicated design. 1.6 inheritanceYou can build a type hierarchy through inheritance to represent a type of problem to be solved. When the existing type is inherited, a new type is created. This new type includes not only all members of the existing type, but also copies the interface of the parent class. That is to say, all messages that can be sent to base class objects can also be sent to export class objects. Because the type of the message sent to the class knows the type of the class, it also means that the export class has the same type as the base class. Because the base class and the export class have the same basic interface, there must be some specific implementations of this interface. That is to say, some code must be executed when the object receives a specific message. There are two ways to make the base class and the export class different: 1 Add a new method to the export Class 2 overriding change the base class method inheritance includes pure substitution (is-) is-like-) 1.7 interchangeable objects accompanied by Polymorphism When dealing with type hierarchies, we often want to treat an object as the specific type to which it belongs, but treat it as the object of its base class, this allows people to write code that does not depend on a specific type. The ability to easily scale the design by exporting new child types is one of the basic ways to encapsulate changes, which can improve the design and reduce the software maintenance cost. When a generalized base class method is called directly, the generalized base class must call the corresponding export type method. But how can I know which export type to call? (Realize polymorphism) through later binding. When a message is sent to an object, the called Code cannot be determined until it is run. The Compiler ensures the existence of the called method and checks the type of the called parameters and return values, but I don't know the exact code to be executed. 1.8 single inheritance StructureAll classes are inherited from the object class. A single inheritance structure ensures that all objects have certain functions. Therefore, you can perform some basic operations on each object in your system. All objects can be easily created on the stack, and parameter passing is greatly simplified. A single inheritance structure makes the implementation of the garbage collector much easier. Because all objects are guaranteed to have type information, the object type cannot be determined, and system-level operations (such as Exception Handling) are particularly important and more flexible. 1.9 containersJava contains various types of containers that meet different needs, such as List (used to store sequences) and MAP (also known as correlated arrays, used to establish associations between objects ), set (each object type only holds one), and more components such as queues, trees, and stacks. In Java Before se5, the objects stored in the container are of the object type. Therefore, the container can store everything and reuse it easily. However, at this time, the container needs to be converted to an object in the upward direction, resulting in loss of type information. During retrieval, you must perform a manual downward transformation, which may cause runtime errors, is unsafe. The appearance of generics solves this problem, such as arraylist <shape> shapes = new arraylist <shape> (). Here, a container is customized to accept and retrieve shape objects only. Generics can avoid transformation errors and time consumption. 1.10 creation and lifecycle of ObjectsJava uses dynamic memory allocation. Whenever you want to create a new object, you need to use the new keyword to build a dynamic instance of this object. Java's garbage collector is designed to handle memory release issues. The garbage collector knows when the object will no longer be used and automatically releases the memory occupied by the object. This is the same as that of all objects inherited from a single base class object and can only be created in one way (created on the stack). These two features are combined to make Java programming much easier. 1.11 Exception HandlingJava has built-in Exception Handling and is mandatory. It is the only acceptable method of error reporting. If you do not write the correct code to handle exceptions, you will get an error message during compilation. 1.12 concurrent programmingSometimes interruptions are necessary for processing tasks with high timeliness. However, for a large number of other problems, we just want to split the problem into multiple parts that can be run independently, this improves the response capability of the program. These parts run independently of each other are called threads. A hidden danger is: sharing resources. Therefore, the entire process is: a task locks its resources, completes its tasks, and then releases the resource lock so that other tasks can use this resource.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.