[Java Programming Ideas-Learning notes] Introduction to the 1th Chapter

Source: Internet
Author: User
Tags export class

1.1 Abstract Process

Java is an object-oriented language, one of its advantages is that only to solve the problem of abstraction, rather than for the specific computer structure , which makes Java has a perfect portability, also known as the Java slogan "Write Once, Run Anywhere."

The so-called abstract process can be understood as modeling the solution problem. For example, to solve the problem is a person, then we can model people, it is the type of people, have attribute name, gender, age, and behavior eat, walk. Java can directly and completely model the code, without regard to the specific computer structure. So when we read the Java program, as the book says, "When you're reading the code that describes the solution, you're reading the problem."

1.2 Properties of the object

As we have observed, people have attribute names and ages, circles have attribute radii, triangles have attribute edge lengths and angles, each object has a certain property , and can be called a property as a member of an object.

1.3 Service for each object

Providing services for each object is also an important idea of object-oriented.

Just like in the human world, building a house is for lodging, making a car for travel, making a school for education, making a hospital for healing. Similarly, the creation of objects in Java is, of course, intended for use by users. If the object being created is not of any use and does not provide any useful service, it becomes a garbage object.

Since each object can serve, it means that the object can do something, and some public methods can be used by other objects, which are also known as interfaces.

1.4 Each object has an interface

Object-oriented has one of the most important ideas-- birds of a feather, each from its class . The universe, whatever it is, is considered an instantiated object, and any instantiated object must be created by a template, which is the class. It should be understood that any living creature can be classified according to "boundaries, doors, outlines, eyes, branches, genera, species", if it is non-biological, such as viruses, stones, sand, etc., but when we call out their names, we also find the class they belong to.

Popular Science Interface definition: Simply put, the interface refers to "What can I do". (You can open the Java API, look at the Java.lang package, inside the interface such as appendable,cloneable,Runnable, suffix are-able, This name also embodies the meaning of the interface)

The question comes, why use the interface? Why does each object have an interface? We might as well take a look at this instanced object of television, when we press the Open button, it receives an "open" request, and then it does some internal work, and the last screen is lit, and when we press a channel, it receives the "next channel" request, then does some internal work, and finally shows the next channel. In fact, each object should have the same working mechanism, such as people eat, people eat a meal, the internal also received the "eat" request, and then is a series of digestion work, and finally will become the energy required for each cell to work. We abstract this working mechanism and thus have the concept of interfaces and implementations. interface, like a button exposed to the outside of the TV, there are open, up/down a channel, volume +/-functions such as buttons; implementation, as hidden in the television inside the complex internal work, it is best to hide it. It is said that each object has an interface, is to avoid the design of the class into a black box, if the TV is not an interface, then we use the remote control can not operate it, what is the purpose of such a TV? So this warns each programmer that designing a class must at least define a common method.

1.5 concrete implementations that are hidden

Why hide the concrete implementation? In addition to the reasons described in section 1.4, there is another important reason. To know that there are two programmers in the programming world, accesses than either (those who create new data types), the client programmers (those who use data types in their applications). Most commonly, it is assumed that the creator of the Java class Library is A1 (the creator of the Class), and that we, the programmers who use the Java class Library, are A2 (that is, the client programmer). A1 and A2 form a relationship, A1 is responsible for building classes, and A2 uses the classes provided by A1 for development. Suppose the class creator developed a class combinationthat computes the number of combinations, and the formula for the mathematically combined number is C (n,m) =m!/n! (M-N)!, we can define two methods, one is to calculate the factorial, the other is to calculate the number of combinations, we do not want to calculate the factorial method becomes an interface exposed, because once exposed, the client programmer can set parameters, modify the internal implementation, so should be a concrete implementation of this kind of hidden, This is to avoid being destroyed, to reduce the bugs in the program, and, in addition, to be a service to the client programmer because they know which methods are available and which are not, and one reason is that when I need to modify a hidden implementation, it doesn't affect the client programmer. For example, I used the loop to achieve factorial calculation, but later I found that with recursion better, I changed, even the method name changed, but the interface of this class is still unchanged, so does not affect the client programmer.

In fact, not only to hide the specific implementation, you should also hide the properties of the object. The hiding of these properties and concrete implementations in object-oriented is known as encapsulation. The meaning of encapsulation is to protect, such as people, he has the attribute age, we all know that age must be non-negative, if not encapsulated, some naughty programmers can be arbitrarily tampered with, such as change to 1 years old, age is negative? Therefore, in order to prevent tampering, the protection of object properties, the introduction of encapsulation.

1.6 Multiplexing Specific implementations

The implementation of Multiplexing is a major feature of Java. This involves the relationship between classes: aggregation, composition, inheritance, association, dependency.

Simply put, the aggregation is the ownership of the relationship, but the life cycle is inconsistent, such as people have a computer is the aggregation relationship, wherein the life of the computer, but the life of the person has not arrived, the life of the person, but the life of the computer has not yet arrived;

A combination is also a relationship of ownership and a strong aggregation relationship, and the life cycle of the two is consistent. For example, people and their brains, neither of whom can leave who;

Inheritance Needless to say, such as Apple inherited from fruit, apple is fruit;
Correlation is also the ownership relationship, the difference is that the association is a one-to-many relationship, such as an order can only have a customer, and a customer has multiple orders;

Dependency is the use of relationships, such as a person to cross the river, he needs to use the ship, in the code is embodied in a class method defines the dependent class of the local variables, or the dependent class as a parameter of such methods.

1.7 Inheritance

Inheritance is a way of reusing code, which is to create a new class based on an existing class, the inherited class is called the base class (or the parent class, the superclass), and the inherited class becomes the export class (or subclass, sub-Class).

There are three cases of inheritance, 1. The export class did nothing, so the export class is exactly the same as the base class ; 2. The Export class overrides (modifies) the method of the base class, that is, the derived class replaces the base class purely; 3. The export class adds a new method and possibly overrides the base class's method, at which point the exported class extends the base class, and in fact we tend to extend the base class when inheriting.

In the inheritance of Java and C + + The biggest difference is the single inheritance, each class can only inherit from a class, and Java all classes are directly or indirectly inherited from the root class Object, as shown in

1.8 Interchangeable objects associated with polymorphism

As shown in section 1.7, we can say that right triangle is a triangle, equilateral triangle is a triangle, but the triangle is not necessarily right triangle or equilateral triangle. Suppose we have designed the triangle inheritance system, then we design a class of Operation Triangle, there is a method to fill the triangle, but there are three kinds of triangles known, namely right triangle, isosceles triangle and equilateral triangle, do you want to overload three kinds of filling methods for these three kinds of triangles respectively? Needless to say, polymorphism makes it possible to accept the generalization of a triangle as long as it is on the parameter, that is, whatever kind of triangle is considered a triangle on the polymorphic, so it can be accepted by the parameters of the method, so it can be filled. For example, the following program:

// do not use polymorphic void Fill (right angle e) {print ("Fill right triangle");} void Fill (isosceles e) {print ("Fill isosceles triangle");} void Fill (equilateral e) {print ("Fill equilateral triangle");} // using polymorphic void Fill (triangle e) {print ("fill" + E);}

It can be seen that the code is more concise due to polymorphic mechanisms.

To this, add an interview to ask questions: encapsulation, inheritance, and polymorphism are object-oriented three characteristics.

1.9 Single-Root inheritance structure

As shown in section 1.7, all classes in Java eventually inherit from a single base class, which is Object. Such a single-root inheritance structure has three advantages:

    1. All objects have a common interface, so that they are essentially the same basic type, and at design time you can also use the adapter mode to solve the interface incompatibility problem;
    2. Ensure that all objects have certain functions, such as the toString (),equal () method;
    3. Makes the garbage collector's implementation easier because it guarantees that each object has its type information.
1.10 Containers

We all know that when declaring an array, you must specify its size, and a specified size can no longer be changed, and the question is, what if the size of the array is still not enough? Can only redefine an array again, this is very inconvenient. To solve this problem, Java provides a container for the container to dynamically adjust the size of the container according to the number of elements it needs. The container has Set,Map,List and Stack, queue, tree, etc., in which the interview is the most love to ask is ArrayList .

1.11 Creation and life cycle of objects

In Java, an object is created as long as the new keyword is used, and then the object is created in the heap, and if the object is created on the stack, the compiler can determine its life cycle and destroy it, but the object is present in the heap, and the compiler cannot determine the object's survival time. In order to destroy objects, free memory, Java provides a garbage collection mechanism, the programmer will no longer need to manually destroy objects, this program is really convenient to write a lot.

Of course, Java is also a disadvantage, because the object is present in the heap, so the creation and operation of the object will be slower, in addition to the destruction of the work of the object to the garbage collector, this kind of a little worry, if the program exits, but the garbage collector has not destroyed the object? So Java programs occupy a world famous for their memory.

1.12 Exception handling: Handling Errors

For example, we design a simple division method divide

int divide (intintreturn x/y;}

At this time if the value of Y is assigned to 0, the program will run an exception, and then apart directly exit, it is obvious that the program experience is too poor, in fact, this situation is not too bad, if it is for the bank, military, aviation design procedures, but did not deal with the abnormal, that look how dangerous. Therefore, in order to ensure the robustness of the program, you must be able to handle the exception.

1.13 Concurrent Programming

Many programming problems require that the program be able to stop the work that is being done and instead solve other problems. Some programs also require the ability to do multiple things at the same time. For example, we want to download multiple resources at the same time, support the continuation of the breakpoint, can pause the download, such as QQ online status, you can chat while viewing the status of friends. You can only use multithreading to solve these problems.

[Java Programming Ideas-Learning notes] Introduction to the 1th Chapter

Related Article

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.