Java Programming Ideas Learn Note 1

Source: Internet
Author: User
Tags export class

I. Introduction to Objects

1. Abstract procedure
Alan Kay has summed up the first successful object-oriented language, and the five basic features of Smalltalk, one of the languages on which Java is based, which represent a purely object-oriented approach to programming
1) everything is Object .
2) the program is a collection of objects that tell each other what they want to do by sending a message . To request an object, you must send a message to the object. More specifically, the message can be imagined as a call request to a method of a particular object.
3) each object has its own storage that is composed of other objects . In other words, you can create a new type of object by creating a package that contains an existing object.
4) each object has its type . According to general parlance, "each object is an instance (instance) of a class ." The most important distinguishing feature of each class from other classes is "what message can be sent to it".
all objects of a particular type can receive the same message .

2. Each object has an interface
All objects are unique, but are also part of the class that has the same attributes and behavior as the object.
Each object can only satisfy certain requests, which are defined by the object's interface (interface) . The interface determines the requests that can be made to a particular object. However, there must be code in the program that satisfies these requests. This code, together with the hidden data, forms the implementation .

3. Service for each object

  think of an object as a "service provider." One of the benefits of doing this: it helps to improve the cohesion of the object. High cohesion is one of the basic quality requirements for software design: This means that the various aspects of a software component are "combined" well.

The problem we often encounter is that we plug too much functionality into one object. In a good object-oriented design, each object can do a good job, but it doesn't try to do more.

4. Specific implementations that are hidden

The program developer is divided by role into * * Class Creator * * and * * Client programmer * *. The goal of the client programmer is to collect a variety of classes for rapid application development. The goal of the class creator is to create the class, which exposes only the necessary parts to the client programmer and hides the other parts.

The first reason for access control is to make it impossible for client programmers to reach out to the parts they should not touch, so they can apply services clearly, and the second reason is to allow the library designer to change how the class works internally without worrying about affecting the client programmer.

5. Multiplexing specific implementations
composition : Using existing classes to synthesize new classes is often considered a "has-a" relationship.
The combination is preferred when creating a new class.

6. Inheritance

You can create a base type to represent the core concepts of some objects in the system, and derive other types from the base types to represent the different ways this core can be implemented.
All messages that can be sent to the base class object can also be sent to the exported class object. This also means that the exported class has the same type as the base class because of the type of the message that is sent to the class, which indicates the type of the class.
There are two ways to make the base class differ from the derived class. The first: Add a new method directly in the export class. This means that the base class does not meet all of your requirements, so you must add new methods, and the second is to change the base class method, which is overwrite (override).
"is a" and "like is a" relationship
Is-a: Inheritance overrides only the method of the base class, and the result can be an export class object to completely replace a base class object. is considered a purely alternative.
Has-a: The new type has an interface of the old type, but also contains other methods, and the base class cannot access the newly added method.

7. Interchangeable objects that accompany polymorphism
When dealing with a hierarchy of types, you typically want to treat an object as the object of its base class instead of treating it as the particular type it belongs to. This allows us to write code that does not depend on a particular type.
A non-object-oriented compiler produces a function call that causes so-called pre-binding , which means that the compiler generates a call to a specific function name, and the runtime resolves the call to the absolute address of the code that will be executed.
The object-oriented programming language uses the concept of late binding . When an object sends a message, the called code is not determined until run time. The compiler ensures that the called method exists and performs type checking on the calling and return values, but does not know the exact code that will be executed. In order to perform late binding, Java uses a small piece of special code instead of an absolute address call. This code uses the information stored in the object to calculate the address of the method body. Thus, each object can behave differently depending on the content of this small piece of code. When a message is sent to an object, the object is able to know what the message should do.
The process of seeing an exported class as its base class is called upward transformation .

8. Single-Root inheritance structure
All classes inherit from object.
A single inheritance structure guarantees that all objects have some functionality.
A single inheritance structure makes the garbage collector's implementation much easier.

9. Containers
Stores an indefinite number of objects.
parameterized types
Before Java SE5, the objects stored by the container had only the common type in Java: Object. Because object can store everything. But this approach is dangerous when it comes to specific applications, because downward transformation is unsafe, and programmers need to spend more time dealing with the right transformation.
The parameterized type mechanism solves this problem. A parameterized type is a class that can be automatically customized to work on a particular type in a compilation. Called the paradigm.

10. Creation and lifetime of objects
How do I know when to destroy these objects? When an object is processed, a part of the system may still be working on it.
Where does the data for the object reside? How do you control the life cycle of an object? There are two ways of doing this:

    • The first is the quest for efficiency, where the storage and life cycle of objects can be determined at the time of writing, by placing objects on the stack (sometimes referred to as automatic or domain-limited variables) or in a static storage area.
    • The second is to create objects dynamically in a pool of memory called heaps. This way, it is not until the runtime knows how many objects, lifecycles, and specific types are needed.

Because storage space is managed dynamically at run time, all that takes a lot of time to allocate storage space in the heap, which can be far greater than the time it takes to create storage space on the stack. Creating storage spaces and freeing up space in the stack usually requires a single assembly instruction, respectively, to move the top pointer down and move the top pointer up. The time to create the heap storage space depends on the design of the storage mechanism.

The dynamic approach has a general logic hypothesis: objects tend to become complex, so the overhead of finding and freeing storage spaces does not have a significant impact on the creation of objects.
Java uses dynamic memory allocation in its full form. Whenever you want to create a new object, use the New keyword to build a dynamic instance of this object.
For languages that allow objects to be created on the stack, the compiler can determine how long an object will survive and automatically destroy it. However, if you create an object on a heap, the compiler knows nothing about its life cycle. As a result, Java provides a mechanism called the garbage collector, which automatically discovers when an object is no longer in use and then destroys it.

11. Exception Handling: Handling Errors
The main problem with most error-handling mechanisms is that they all rely on the programmer's own vigilance, which comes from a common convention rather than the one that the programming language enforces.
Exception handling directly puts error handling directly into the programming language, sometimes even in the operating system.
Note that exception handling is not an object-oriented language feature.

12. Concurrent Programming
There is a basic concept in computer programming, which is the idea of dealing with multiple tasks at the same time. Many programming problems require that the program be able to stop the work being done, turn to some other problem, and then return to the main process.
We can improve the responsiveness of the program by splitting the task into several separate parts. These concepts are called "concurrency" in the program, which are called threads by the parts that run independently of each other.
But there is one hidden danger: sharing resources . If there are multiple parallel tasks that have access to the same resource, there will be a problem. The problem is solved by locking a resource, completing its tasks, and releasing resources so that other tasks can use the resource.

13.Java and the Internet
Waiting to be mended .... I can't read it.

14. Summary

The Java program contains only the following two parts: objects that represent the concept of the problem space (not related to how the computer is represented), and messages that are sent to these objects to represent the behavior in this space .

The content of this chapter is very extensive, but also needs to be fully studied and then come back to the overall grasp of the essence.

Java Programming Ideas Learn Note 1

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.