& Lt; java programming Ideology & gt; reading pen I, java programming ideology

Source: Internet
Author: User

<Java programming ideology> reading pen and writing notes, java programming ideology

I have been working for a year. Haven't I read the Bible? Isn't that funny? I finally bought this book last week. I 've been reading it for the past two days. I read it and take notes again, I wrote something that I think is humorous. On the one hand, I wrote something with relish. On the other hand, I also hope that I can give readers a smile. In this article, I am only talking about it, if there are any errors, you are welcome to correct them. The landlord will modify them carefully. Start as follows:

  

1. abstract process  

At the beginning of the book, I wrote that every programming language provides an abstract mechanism. Next I will introduce two abstract mechanisms, but I have never touched the language in it, so I will not go into details, the abstract mechanism of object-oriented language is to abstract objects. So what is an object? An object can be viewed as a computer with its own State and operations. You can ask the object to perform these operations. For example, for the cola bottle on my desk, the capacity is 600 ML, and the capacity is the property of the bottle, that is, the status. You can install cola, that is, its method, that is, its operation, cola bottles can be used to install cola. Of course, messages need to be sent, that is, the call method.

Five Basic Features of object-oriented language:
Everything is an object

In the book, objects can be viewed as a strange kind of energy that can store data and require him to perform operations on himself. My understanding is that such a thing or a thing has its own attributes and has some behaviors, so it can be called an object,

The cola bottle has a capacity and can be loaded with cola, so it is an object (forgive the thinking of a person who likes to drink Cola). The display screen has a size and can display graphic images, it is also an object. An atom has its own nucleus and can constitute a molecule. Then, an atom is also an object ......

A program is a collection of objects. They send messages to each other to tell each other what to do.

The so-called message is a call request to a method of an object. If you call a simple program, you and the phone are two objects in the program, if you want to make a call, you need to tell the phone that I want to make a call, but the phone is not a human or a god. He doesn't understand what you say, what you need to do is to press the phone number on the keyboard and then press the call key. That is to say, you have called the call method of the phone object as an object, simply put, the program requires the objects in the program to call each other's methods as needed.

Each object has its own storage composed of other objects.

In fact, I don't quite understand this article. According to the book, we can create new objects in the form of existing object packages. This is understandable, just like a person is an object, A group of people can also be seen as objects, but what do I not understand? However, if this object is a person who has height and weight, he can walk and talk, but how does it contain storage composed of other objects? Can this be understood? This person needs to wear clothes and clothes can also be seen as an object. So this person object contains the storage made up of clothes objects? Please be confused.

Each object has its type

This is also inevitable. An old man is a person and a child is also a person. They are all human beings, but can you say that a mermaid is a person? No, you can only say that a mermaid is a fish because it is a fish.

All objects of a specific type can receive the same message.

This is very understandable, that is, as long as the object type is human, whether it is a man, a woman or an old man or a child, as long as it is a person, you can walk and speak (of course, I am talking about the general situation, if you really want to say that this person has no legs or that this person is dumb, then I cannot), that is to say, these types of people can call the walking and speaking methods.

Booch describes the object as follows:The object has the status, behavior, and identifier.What is the object status? Is the property of the object itself, such as the size of the cola bottle, the behavior is the method of the object, the definition of the identity in the book is that each object has a unique address in the memory, however, I think the identifier is used to differentiate objects, but it does not need to be distinguished from the storage address. You can distinguish from the object type first, A child object cannot be the same as a cola object, but if it is a child object and an elderly object, it cannot be differentiated by type, because both types are human objects, at this time, we need to distinguish from the storage address.

2. Each object has an Interface

Before explaining the object interface, This article first defines the class. What is a class? A class, that is, a set of objects with the same features and behaviors, or, conversely, each object is unique, but it is also a part of the class that objects with the same features and behaviors belong to. The class is represented by the keyword class. After creating a class, you can create countless Objects Based on this class, just as you have created a cola class, then you can create Pepsi objects and Coca-Cola objects (forgive me for not thinking about other cola objects), and then you can operate on the cola objects you have created, but how can we get useful objects? Or how can we make the cool object install only Cola instead of cool? That is to say, an object only meets certain requests. These requests are defined by the object interface, and the interface is determined by the class or type we define. What does it mean? Example of cola: Now we define a cola class named KeLe. There is a method named zhuang () in the class, and then KeLe baishi = new KeLe () baishi. zhuang (), we used new to create the baishi object. This object can call the zhuang method, but do we need to understand that this zhuang () method can be used to hold boiled water? Can it be used for installation? No. It can only be used to hold cola. Why? Because the baishi object is created based on the KeLe class, this class is Cola class, so his method can only be to install Cola instead of others, this further demonstrates that the interface mentioned in the book is determined by the type. In addition, the book said that the interface determines the requests that a specific object can send. This is understandable, as you mentioned above, can you use the baishi object to send requests for water? Or can you use Zhang San's phone number to call Li Si? That is to say, the interface is used to limit what objects can do, which is closely related to methods in classes and classes.

 

3. Each object provides services

This is still an example of the use of cola. Why? Isn't that nonsense? Why is it so cool to drink cola? It is important to care about cola ingredients, sugar, water, dyeing agents, and so on. It is these ingredients that have made Cola together. This is the same as the program. The program is used to serve users, however, it needs to call the services of other objects to implement this parent program. Just like the right ingredients can be used with cola, the right objects can achieve perfect programs, as a programmer, you need to create or find a series of objects that are appropriate or provide ideal services to solve the problem, break down the problems you need to solve or write programs into a combination of a series of objects. At the same time, the book also said that some of the benefits of seeing objects as service providers. What are the benefits? To improve object cohesion, high cohesion is one of the basic quality requirements of software design: this means that a software component is well combined in all aspects. How can this be understood? Or cola, we all know that sugar is the sweetness of cola, and water is the dilution of sugar to keep cola liquid (the above is purely fictitious, if there are similar, go to the Coca-Cola Company ), but now, if I don't add water, I just need to boil sugar and add it. Is it better to drink this cola? I want to say, brother, can you still drink it? What does this example mean? That is to say, you just need to complete a task for each type of things. Don't do too much. If you have more people, you may not be able to get your feelings. If you are a sugar, you can quietly provide your sweetness. What kind of dilution do you want to do? It is the responsibility of water. Just like programming, it is enough for an object to provide a single responsibility and do not need to bear more responsibilities. Otherwise, it will be good for anyone who can't help but break up a pot of porridge, this is what we often call a single responsibility issue. If your target provides useful services and is designed according to a single responsibility, it is easier for others to understand your code or reuse your code.

4. Specific implementation of hiding

In the book, developers are divided into two types by role. One is the creator of the new data type, and the other is the consumer of the data type, what is the specific implementation of the hidden object? This means that the class creator creates a new class, but only provides the interface to the client programmer and does not tell the client programmer how to implement the class, this is also the interface-oriented programming we often mention, that is, I only tell you what this class can do, but how can I do it? Just like I have invented a new type of cola, I will tell you that sugar and water can be used to produce pure natural green and pollution-free cola, but will water be heated or sugar be frozen? I will not tell you, what do you want me to do? If you provide water and sugar, I will give you cola. What do you do? The same is true for programming. I will provide an interface. As long as you provide the parameters required by the interface, my interface will return the data you need, it does not matter how it is implemented.

What are the benefits of doing so? First, avoid the client program from modifying the places that shouldn't have been modified. My algorithm is like this. Only in this way can the result be obtained. If someone else can modify my implementation, different results are returned? I don't know how to hide the hidden part or the fragile part in the implementation, so that the caller is not allowed to modify it at will. Second, your modifications to the hidden part will not affect the caller. If you provide an implementation class instead of an interface to the caller, the caller will not change your implementation method and call it normally, this kind of thing is also important to you. People use thousands of places at a time. If you look back, no, I have to change this place. You have to change it, when the caller tests the program, the system instantly explodes and completes the kill process. Do you mean the caller will be rushing to your home with a knife? If you separate interfaces from implementations, how can you change your implementations without affecting people who call them?

I will not talk about the modifier scope here. As we all know, there will be more nonsense and more nonsense.

5. Reuse specific implementation

Code reuse I believe everyone knows that, for example, we usually write a date conversion method in the tool class and directly call this method when using it. Of course, this is method reuse, the reuse mentioned in the book is the reuse of classes. What is the reuse of classes? I also said that there are two families selling cola, one selling Pepsi and the other selling Coca-Cola. If you want to create a class at this time, do you want to create a cola class or create one for the two cola classes? Isn't that nonsense? With my many years of programming experience, I don't know how to create a cola class, and then create the Pepsi and Coca-Cola objects respectively, this is the simplest method of reusing classes-directly using an object of classes, that is, using class objects to achieve the purpose of reusing classes. This is one of them.

So what is the second point? Let's talk about cola first. one of the bosses thinks that selling cola alone won't make money. What should I do? I have changed the sales package. What is the package? Fresh chicken legs, fine dishes, and a cola (small) Cup. How can this problem be solved? You need to create a new meal class with the following attributes: Chicken leg size, chicken leg color, Cola size, and cola type ...... Is it a hodgedge? Are there too many class management tasks in your hodgedge that do not conform to the single responsibility principle we have mentioned? What should I do? In this case, the second method of reuse is required: place an object of the class in a new class. In this case, you can create a cola object first, then you only need to use a set of meals to include cola objects. I only need to know whether you are Pepsi or delicious, as for the attributes of cola, you just need to give it to the cola class for maintenance. I don't need to know, like other chicken legs and pickles, the result is that the meal type only needs to know what exists in me. As for their respective attributes, the maintenance of their classes should be completed. Does this not violate the single responsibility principle?

A combination of any number of objects that can implement the desired functions of a new class is called a combination. If a combination occurs dynamically, so it is also called aggregation (you can use Baidu for the difference between combination and aggregation). a combination is regarded as a "has-a" relationship, that is, a relationship of ownership, just as a person has hands and feet, let's talk about the relationship between combination and inheritance in the next section.

6. Inheritance

Now, on the subway, I will use a cell phone code to talk about inheritance. The book is not around. I hope it will not be wrong. First, why does inheritance appear? As we have mentioned above, everything is an object, and every object is an instance of a certain class, so now we are still the example of cola. We all know that there are Pepsi and Coca Cola, so well, now we can create two classes: Pepsi BaiShiClass and Coca-Cola KeKouClass, both of which have two attributes: price and capacity, but we need to know two problems: first, both Pepsi and Coca-Cola are cola; second, there are only two types of cola. If the third and fourth types of cola or even the nth cola appear, do we need to create a class for each cola, what about the price and capacity attributes in their respective classes? Do you think this is wrong? No, this is understandable, but it is troublesome. For programmers who are afraid of trouble, it means you have to work overtime. How can this problem be solved? The clever java designer gave us an inheritance (the above is simply to lead to the inheritance of YY, the specific inheritance, please be independent), how to do inheritance? It is easy to create a KeLe class as the parent class (base class), and then let the specific cola class inherit the parent class we created, after inheritance, the Child class automatically has attributes and behaviors of the parent class. What does that mean? That is to say, if you call your father Huang AMA, and your father retired, you have become an emperor. The people who used to call your father the emperor now have to call you the Emperor, this is what you mean when the old man wore the robe now. As long as the Child class inherits the parent class, the Child class has everything of the parent class. At this time, some people will have such questions. As you said, if your Pepsi inherits cola and your Coca-Cola inherits cola, isn't both of them confused, you have forced the merger of Pepsi and Coca-Cola companies. Why? Why? I also want to explain that the subclass inherits the parent class and has all attributes and methods of the parent class, but the subclass can also have its own attributes and methods, if the subclass does not have its own attributes or methods, this is the most basic inheritance. The relationship between the subclass and its parent class is "is-a". In short, the subclass is the parent class, can you say that Pepsi is not cola? Can you say that Coca-Cola is not cola? However, if the subclass still has its own attributes and methods when inheriting the parent class, the Child class and the parent class are in the "is-like-a" relationship. The Child class is like a parent class, but it is not exactly the same as the father class. It is also easy to understand that Tang Gaozu Li Yuan is the emperor, his son Tang Taizong Li Shimin is also the emperor, and Taizong inherits the Emperor's throne. What are the similarities between the two? The similarities are the emperors, and the differences are also very obvious. So we can only say that the two are similar, but can you say that this is an emperor? Another point is that the subclass inherits the methods and attributes of the parent class, but the subclass also has the right to override the inherited methods. What does it mean? For example, if your father likes to eat noodles, but you like to eat rice, it is also a meal, but you two eat different things, or you like to eat in a pot, I like to eat in a basin. Isn't that true? Therefore, subclasses can have methods of the parent class, but methods can be rewritten, and do not have to follow the conventions.

I wrote it here for the time being. If you say something wrong, you are welcome to correct it. The landlord will continue to work hard.

 

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.