18.2 Important Concepts in Object-Oriented Software Development In Chapter 4 I have already introduced what is an object-oriented software development, you should still remember the example of "people" as an example, and when the road met evil dog When this incident is happened by the evil dog, he can call "racing" and run "as a response to this event.
But no matter what, I don't want to make you tapping the object-oriented software development. In fact, object-oriented meaning is far-reaching, involving many important concepts, only fully understands the meaning of these concepts, you really understand what is object-oriented. Many concepts you should know, but limited to space, I will explain the most important nine in this section.
Object-oriented software development nine most basic concepts: class, objects, attributes, methods, abstractions, packages, inheritance, polymorphism, and events.
18.2.1 Classs and objects
Class is the software of the object, which is a template for creating an object. For example, if a person is seen as an object, the species can be seen as a class. Class definitions describe structures including data and functions, and objects are created by them. A class represents a set of similar objects. Object is an example of a class.
Why do you need a class, imagine a situation, if we want to write a flash game, there are some small people who can act independent, these villains have the same physical characteristics and activity capabilities, then, if we want for each small If a person writes a program, the workload is undoubtedly huge and repeated. In this case, we should create a "small person" class, which will generate a small person from the "small" class whenever a new small person is needed.
It is said that you must think of the relationship between components and instances. That's right, the relationship between classes, and objects is the same as the relationship between components and instances, because the components are actually the built-in class of Flash.
18.2.2 Properties and Methods
The attribute is data, and the method is a function. Attribute is something you know, and the method is the class complete. The properties and methods are the responsibilities of the class. Object-oriented software development is based on this concept: the system should be created by an object, the object has data and functions. The property defines the data, and the method is defined.
Obviously, in an object-oriented development, the most important job is to define the class. When you define a class, you must define its properties and methods. The definition of attributes should be directly clear and need to define its name and data type. The definition of the method is the process of creating a function. As needed, you can create a method of accepting parameters and returning values.
18.2.3 Abstract
The world is very complicated, in order to deal with its complexity, in software design, when we need to establish mathematical models on things, we need to generalize or abstract things. Or people as an object, from the perspective of recruitment, we need to know the name, gender, age, educational background, work experience, contact information, and personality characteristics; and from the perspective of blind date, you need to know this person. Age, height, appearance, educational background, family background, and even eight characters. Still the same person, just a different abstraction of him (or her), what abstract to a matter will depend on the procedure you want to design.
Abstraction is a process of analyzing, which is the process of drawing a clear frame around a thing. Abstract should contain features, attributes, methods that are interested in applications, and ignore other factors. That's why recruiting abstraction contains educational background and work experience, but will not contain the reason for the birth. The abstract process is to define the process of knowing and going to finish.
18.2.4 Package
Although abstraction tells us that for recruitment, we should store the candidate's educational background and work experience, but it did not tell us how to complete these things. The encapsulation solves how to model the system function. In the object-oriented world, we have to model the system into classes, and the classes must also be modeled as attributes and methods. The process of designing how to accomplish these things is called package. The package describes how to divide the function in the system. We don't need to know how the object is implemented. The package suggests that we can build a system in any way. If necessary, it can also modify its internal structure again in the future, as long as the interface between the different function components in the system does not change, the change in the system is not changed. Other features of the system will have an impact.
The package is a process of applying the frame around the thing. You can define anything you want to define, but you don't need to inform the outside world. For example, when you go to the bank to access cash, you don't need to know how the bank's data center uses mainframes, small machines, and PCs to record your account information, how do they use the database and operating systems, It is also important because they have already encapsulated the functionality of the account service. You only need to go to the counter, you can pay the bank's staff. By hiding the details of the implementation of the account function, banks can freely change the functionality at any time without having to replace the passbook of each user.
In order to make the application easy to maintain and increase security and robustness, access to the properties and methods of the class is required. Basic ideas are as follows: If a class wants to get information about another class, you must first conscription, instead of it directly. Considering that in the real world is actually working in this way. If you want to know the name of other people, what should you do? Is it directly asked about his name, or stealing his wallet to see his ID card?
18.2.5 inheritance
There is often similarity between different classes. Two or more classes also share the same properties and / or the same approach. Because we don't want to repeat the code, we will use this similar mechanism. Inheritance is this mechanism, which makes you easy to reuse existing data and code.
For example, students have name, address, mobile phone, can also run quickly. At the same time, teachers have these things. There is no doubt that we can develop classes about students and teachers to run them together. In fact, just develop a Student class first, once it runs, make a copy, just call the Teacher class, and make some appropriate modifications. This is easy, but it is not perfect. What happens if the source code in the Student class occurs? You will have to correct mistakes in two places. This will be a borus work. If only one code copy is used for development and maintenance, isn't this better?
This is the idea of inheritance. With inheritance, you can define a class to encapsulate the similarity between students and teachers. The new class will have attribute Name, Address, MobileTelephone, and method Run. We can name this class as Person.
Once the Person class is defined, the Student and Teacher class can be inherited from it. We call Person's superclass for Student and Teacher classes, while Student and Teacher classes are subclasses of the Person class. Any super class knows and completed things, the subclass knows. Therefore, since the Person class is able to race when you encounter a disgusted dog, Student and Teacher class can also.
18.2.6 polymorphism
Let us consider this example. You love your girlfriend, you say "Dear, I Love You", your girlfriend hugs you, you have got a kiss, you think this is very good. Then, you came to the street. I saw a very temperament beauty. You also said that "Dear, I love you" with my girlfriend, you will immediately get your nerves and morality. Some suggestions for aspects. Subsequently, you came to the hospital, say "Dear, I love you" for an old Chinese medicine, and the old Chinese medicine will not hesitate to pull your wrist, starting to put the pulse for you. You think, wait until when I encounter these objects and say "Dear, I love you", this situation will definitely happen - this is a polymorphism. Polymorphism allows objects to collaborate with other objects when you don't know the type of other objects in advance.
From this experience you can learn a few interesting experience:
First, polymorphism hinders your interaction with other objects. You don't distinguish the type of other objects, you treat them in the same way. Your idea is that other objects are people. Polymorphism is a concept that allows this situation.
Second, different objects do different responses in their own way. Your girlfriend hugs you and give you a kiss; strange beauty gives you advice on nerves and morality; and old Chinese medicine gives you the pulse. The same message reaches different objects, each of which is different. In fact, from the perspective of each object, they do what they should do.
Third, the polymorphism can achieve a consistent and appropriate method name. Although each object responds to "Dear, I Love You" in an appropriate manner, but it is still necessary to implement the different versions of the method.
The polymorphism mainly includes two aspects:
Operator's overload: The same operator can act on a variety of object types.
Function name overload: The same function name can act on different object types and produce different effects.
18.2.7 Event
The operation of object-oriented software is performed as an event, which is called "event driver". For example, when you appreciate the flash animation, it is actually a "enter frame" event, in which these events, the Flash Player will deliver the content in each frame, so you can see the picture.
Similarly, when you click on the button, a button event occurs, the script in the button event is executed, the role of these scripts may be to bring you to a specific scenario, a specific frame, or link to a certain URL.
In object-oriented programming, the entire software is driven by events, which means that if there is no incident, your program will be difficult, but it always has some things in the program, even if all others There will be no events, and there will be time to happen at all times.
In the actual development in this book, you will see the actual use of all of these object-oriented concepts.