7. Interchangeable objects that accompany polymorphism
In front of the inheritance, we know that there is a base class (parent class) and export Class (subclass) in the inheritance, do not know if you have encountered this situation? When passing an object as a parameter to a method, we often choose to pass a base class instead of a subclass, why do we do this? In fact, the reason is very simple, said the tall point is that this is conducive to the robustness of code and extensibility, said in detail is conducive to the robustness of the Code and extensibility, more importantly, extensibility.
Also take the example of drinking cola, if you pass the Parameter object is cola, then whether you give me Pepsi or Coca-Cola I can accept ah, but if you pass the parameter is only Pepsi, but I just want to drink Coca-Cola today? At this time, I am afraid you can only write an exact same method, and then passed the Coca-Cola parameters, but only two Coke, add I want to drink is a drink, so many drinks you write a method of each? That is unrealistic, what is the best solution? That is, in the book, the upward transformation, the transfer of parameters when the type of generalization, that is, we call the base class, if there is a new type to add the time only need to inherit the base class, then add a new type when the method is not affected by the call. That's what the book says. When dealing with the hierarchy of classes, we often do not want to treat an object as a particular type to which it belongs, but as an object of its base class, so that it is convenient to extend the new type, and that extending the new type has no effect on the invocation of an existing method.
But there is also a problem when you treat an object as the object of its base class, what is the problem? We are people, can drink out which is Pepsi which is Coca-Cola, but the compiler is a machine, it does not know what you pass in the object is what, it only know that you are introduced to Coke, but in the end is Pepsi or Coca-Cola, it does not know, so it can not execute the corresponding code to produce corresponding results, how to do? This time the book put forward the concept of late binding, what is late binding it? When the compiler says no, I don't know what code to execute, I know the arguments passed to me must be of this type, and I can give you a fixed return, and I don't know what else to ask. How do you understand this sentence? Like drinking coke, I don't care what kind of coke you give me, anyway you have to give me a bottle of Coke, drink Coke I will feel cool, and you can play happily, that is the meaning. But the implementation of late binding also requires a small piece of code, the book said to the eighth chapter in detail to expand, this small piece of code is what I do not know, then back to solve the problem, and now I am not very clear.
The final part of this section is the use of geometric examples of the next polymorphic, saying that the late binding can also be called dynamic binding, in Java This is the default behavior, there is no need for additional action to open dynamic binding, and dynamic binding is also necessary to implement polymorphism.
8. Single-Root inheritance structure
There is only one root inheritance in Java, which is what we call single inheritance, that is, a class can have only one parent class, cannot have more than one parent, just as your biological father can only have the same, no more. In Java all classes eventually inherit from a single base class object, which is what we usually say all of the objects can be regarded as an object, so what is the benefit of the individual inheritance?
The benefits are mainly three points, one of which is that all objects have a common interface, which is the same basic type. What do you mean? The meaning is Zhang three is a person, John Doe is also a person, Harry or a person, the programming inside is a man is a object,animal is also an object, all objects are an object in the final analysis, That is, you can use object instead of any object you want, or any object you want can use object to occupy the position. This is the 1th advantage, so what is the 2nd advantage? The 2nd advantage is that all objects have some function, we look at the JDK API at any point to open a class, in the class method column we will see a clone () method, which means that any object we can call the Clone () method, of course, there are some other common methods, These methods ensure that you can perform some operations on any object. Third, a single root inheritance to make garbage collection has become a lot simpler, for the garbage collection of my knowledge is also relatively small, so it is not caught dead, and later have the opportunity to study garbage recycling this piece.
9. Containers
As the name implies, the container, of course, is used to carry things, Coke bottle is a container, take to install Coke, but the container in Java is not brought to install Coke, about this we need to know two points, one, Java inside the container is not something, but the object, the exact word should be the object of reference, second, The container inside Java holds references to other objects, but it is still an object. In addition, in Java we call the container a set, I believe you also know that the set framework in Java is a very important part of the content, the more common collection has list,map,set these three,
They are applied in different scenarios, this is the time to learn the set frame, we are in detail, but there is one thing we need to know now, that is, since the collection is a reference to hold other objects of an object, the straightforward point is that now there is a person, you can represent a group of people, something happened you carry, But why design so many collections? Why not just design a list, but also design what map ah set ah these? There are two main reasons for this, and 1th, different sets provide different types of interfaces and external behaviors, such as list to store sequences, but map is used to store key-value pairs, they work differently, and 2nd, different sets have different efficiencies for some operations. such as the comparison of ArrayList and LinkedList.
You may have seen the list<string> list = new Arraylist<string> () that creates the collection, so why put a string in the middle of the angle brackets, in fact, the string here can be replaced by any type, Can even be changed to object, in fact, before the Java SE5 is not list<string> this way, can only be written as List, but after the Java SE5 the concept of generics,list<string> is a set with a generic type, What does that mean? It means that in this list you can only deposit variables of type string, the other compiler is not recognized, like a warehouse can only take money, if you put the white paper in the janitor won't let you in the same. Why would you want to add generics? This is because before Java SE5 write a list, do you know what is in it? If this list is created by you, you also know that you have deposited a string object inside, but others don't know what to do if someone is depositing a digital object? And there is no error when it is saved, but when it gets out, it's a problem, what's the problem? You save a string, the second person saves a number, the third person saves a human object, but you get it by the string object, you say can not be wrong? A collection without generics is like a collection of list<objetc>, as long as you are an object, and I do not want to, we all know that all objects inherit from object, so even if you deposit a number object, it will also be converted upward to object, This is completely no problem, but when you take out this object is the downward transformation, we need to know that the upward transformation is not a problem, but the downward transformation, even if you know exactly what type is the case you also have to convert the object type, if you do not know the type of object, It is very unsafe, for the above reasons, Java SE5 later added the concept of generics, it is equivalent to the set can be stored in the type, so that the collection can only be stored in this type of object, there will be no upward transformation and downward transformation of the problem.
The first chapter of the section is written here, the object of the creation and life cycle of this section I suppressed two days did not write a word, because the simple reading in the interpretation of the feeling is very obscure, do not know how to understand, let alone in their own language expression, mainly my personal understanding of this piece is not in place, I also went online to check this part of the content, the object of the life cycle of the article are written very well, we can refer to, and so I have the ability to control this piece of time to improve this article, as for the following anomalies and concurrent programming and internate originally I did not intend to write in detail, Because the book in the three parts are simple introduction, and so see the corresponding chapters when the detailed understanding.
Finally want to say that want to write something really difficult, the book to see faster, but write the article is much slower, a week is to come out an article. Keep working hard and try to write the second chapter next week.
<java Programming Ideas > Chapter One reading notes two