Object-oriented way of thinking (reprint, author: Unnamed)

zhaozj2021-02-16  124

1. Related to the term Object-Oriented Programming OOP Object-Oriented Program Design

2. Why is OOP to be thriving so long? It is proven to be a successful way to solve problems from small to large scale and solve problems in other fields.

3. Example OOP is often described as a new example. Example (paradigm) refers to an exemplary model or example, which provides a form of organizational information

4. Traditional programming and computing models are used to describe the traditional model of the computer executor is called the process status model, or called a pigeon cage model. It mainly considers: status, variable, assignment, and loop and other computers are a data manager. It follows a set of design instructions, moving back and forth in memory, remove data from multiple slots (memory addresses), with some kind The way to change their state, then put the result into other slots by checking the values ​​in the slot, determine the status or calculation of the computer. Although this model is more or less describes what happened inside, it can't help us understand how the computer solves the problem, it is not most people (except for the owner of the pigeon and post workers) to solve the problem. Methods.

5. Recursive: The program consists of a small similar computing component in the 1970s, and Alan Kay considers the computer's traditional design, and proposes a program that can be composed of a plurality of small similar computing components. If each part is viewed as an agent, the program can be constructed from a small computing agent.

6. OOP Definition Alan Kay Definition of OOP: OOP is based on the principle of recursive design: --1. Everything is an object. --2. Calculating inter-object inter-communication, requiring other objects to perform actions. The object is communicated between the objects by sending and receiving messages. --3. Each object has its own memory, which may include other objects. --4. Each object is an instance of a class. Class is a set of similar objects. --5. Class is a repository for object-related behavior. That is, all objects of the same class can perform the same action. --6. Class is organized into a tree structure with a single root node, called inheritance hierarchy. Memory and behavior associated with class instances are automatically inherited by future generations in the tree structure.

7 OOP concept: Give friends to send flowers: I want to send flowers to live in the other city Sally. I can't send these flowers directly, so I will use the local flower service system. I told the Sally's address, the price and breed of the flower, and Flora contact Sally's flower merchants in the city of Sally, the latter prepares these flowers and contacts the flowers. In-depth research, you can find more people to participate, such as flower farmers, planting gardens, etc. 7.1 OOP Concept: A proxy and team-oriented program consists of an interacting agent group that is called object. Each object assists a role. Each object provides a service or an action to serve other objects in a group. 7.2 OOP Element: Object OOP Article 1 Principle:: LOL: 1. Everything is an object. The action in OOP is done by the agent, which we call "instances" or "objects". In our settings, there are many agents to work together include: I, Sally Flowers, flowers, floral wholesalers, flower field owners, flowers, flowers, flowers, flowers, flowers, flowers. Every agent has to play a role, the result is that when all work is finished, our purpose is reached. 7.3 OOP Element: Message: LOL: OOP Article 2 Principles: 2. Calculate inter-access communication, request other objects to perform action. The object is communicated between the objects by sending and receiving messages. The action in OOP is done by the response to the action request (called message, Messenges). An instance can receive a message and then complete an action and return a value. To complete the process of sending flowers, I sent a message to Flora, and Flora then issued a message to the flower business in the city of Sally, the latter issued a message to the flowers, ... 7.4 Information Hide Principles Note, as an object One user of the service, I just need to know the name of the message will accept. I don't need to know what actions are needed to do. After receiving a message, the object is responsible for completing the task. 7.5 OOP element: The recipient message is sent to the receiver receiving this message. Different recipients may differ from the interpretation of the message. 7.6 Different from the delivery object, the explanation of the message may also be different var Flora: Florist; Elizabeth: Friend; Kenneth: Dentist; begin flora.sendflowersto (Sally); {can complete} elizabeth.sendflowersto (Sally); { Kenneth.sendflowersTo (Sally); {perhaps} END; the same message, depending on the delivery object, it may get different results. 7.7 Behavior, the later binding different objects Even if the same message is received, the action (behavior, behavior) that the object is completed is also different. Determine what kind of behavior of the completion can be done at the time of operation, which is called later binding. The same name indicates a different form of operation, which is a form of polymorphism. 7.8 OOP Elements: Recursive Design: LOL: OOP Article 3 Principle: 3. Each object has its own memory, which may include other objects.

7.9 Do not interfere with the object to complete the task in any way it thinks the appropriate non-interference, not to intervene. 7.10 OOP Elements: Class OOP Article 4, 5 Principles: LOL: 4. Each object is an instance of a class. Class is a set of similar objects: LOL: 5. Class is a repository for object-related behavior (Repository). That is, all objects of the same class can perform the same action. The expectations of Flora behavior are based on the general behavior of all flower merchants to determine that Flora is an instance of the Florist (Class) (Instance)

The behavior is associated with a class, not contact with a single instance. All objects belonging to an instance of a certain type, the same method is taken to respond to similar messages. 7.11 The level of category knows that Flora is a flower business, I also know that he is a businessman, human, mammal, material object. At each level, you can understand specific information, which is available for all lower levels. 7.12 class level 7.13 OOP element: inheritance: LOL: OOP Article 6 Principle: 6. Class is organized into a tree structure with a single root node, called inheritance hierarchy. Memory and behavior associated with class instances are automatically inherited by future generations in the tree structure. Information (data, behavior) associated with a layer is automatically provided in the lower level of this hierarchy in the class hierarchy. 7.14 OOP Element: The cover can change its information from the parent class: All mammals are the fetal vests of the birth of the birth of the egg, which combines ovate mammal inheritance, reflecting the object-oriented huge capacity. 2 lesson

Abstract definitions, refers to intentionally compress or hide the process or product details to get clearer performance, detail, or structure. Abstract is the most important tool when controlling complexity.

Abstract instance: Gent Atlas If you open a local gather, you generally see a world map. The map only shows some of the most important features, such as the main mountains, oceans, etc., but details are basically ignored. A series of maps will cover small geographic areas and process more details. For example, a map of a continent (such as continents) will include the border and major countries of the country. Smaller areas (such as country) maps will include the name of the city, mountain peaks. A city map may include the main road to enter and exit, and some maps will even draw some buildings.

Each abstraction hierarchy includes some information, and some information is ignored. We note that at each level, some information is included, while others are ignored. This is only because of the higher level, it cannot express all details. Even if it is capable, no one can handle such a large amount of information. Therefore, many details are ignored. People usually use some simple tools to build, understand and manage complex systems. The most important technique is called "abstraction).

Information hidden information hide refers to the intentional details of the establishment of abstract representation. Information hide makes abstraction to control complexity.

Abstract hierarchy in a typical OOP program, there are many levels of abstraction. Wherein, the highest level is part of the program to be objects. At the highest level, the program is considered a "community" of an object, which interacts between these objects to complete a common goal.

Group: Two-story meaning in the development of object-oriented program development, there are two levels of "group": first is a group of components, they interact in the real world to develop applications. The second group is a group of the objects created by these programmers, which interacts in the virtual world to complete their common goals. The key is that information is hidden, and every level is abstract

Abstract The highest levels, all objects to stand out in groups and partners, provide services that are used by other members in the organization. At the highest level of abstract, the most important thing is to highlight groups and cooperation, as well as how members interact with other members. Many languages ​​allow for synergistic objects to be combined into a "unit" (Unit). For example, Java's "Pack" package, "Name Spaces" Name Spaces, "Units" in Delphi. These units reveal some features to the world outside the unit, while other features are hidden in the unit. Two layers of abstraction: The two layers of abstracism between the two layers of the two objects: an object (Server, Server) provides a service to another object (customer, client), and the two interacts between communication.

Terminology Usage: Server and Client The Server here does not refer to servers (such as website servers, web server), but refers to servers who provide services. This abstraction involves two perspectives of a relationship: from the perspective of the side of the customer, and from the perspective of the server.

In a well-oriented style design, we do not have to consider the abstraction of the level when using these services, in describing and discussing the service providing services, how to implement abstract behavior is ultimately The level of abstraction is a method that implements the sequence of operations required to complete the task.

Determine the abstraction of the appropriate level in the early days of software development, the key issue is to determine the abstraction of the appropriate level. Don't ignore too much details, don't include too much details.

Abstract form: An abstract form commonly used in granted method is to divide a layer into multiple components. For example, a car is composed of an engine, a transmission mechanism, a body, and a wheel. To understand the concept of the car, just check the components in turn. This is the traditional division and conquer.

Abstract form: specialization (avatars, specialized) Another abstraction is called specialization or specificization. For example, the car is a wheel-type carrier, and a wheel carrier is a carrier tool. . We understand the knowledge of wheel-carrying tools, equally applicable to cars and bicycles. The knowledge we know about the vehicle is equally applicable to horses and bicycles. Object-oriented language often use this form of abstraction

Abstract form: Another form of different perspectives is a different perspective of the same item. Each perspective emphasizes some details to ignore other details, so different features are described for the same object. For example, the car and the car in the eyes of the public eyes are very different.

"It's a" "There is a" abstract grahe method and specialization, representing the most commonly used forms of object-oriented languages, generally referred to as "is a" (IS-a) and "there is a" (HAS " -a) abstraction. Turn method: "There is a" abstract grahe method "has a" abstraction. For example, a car "has a" engine, "has a" transmission mechanism, and so on. Specialization: "It is an abstract specialization" is an abstraction. For example, bicycle "is a" wheel carrier tool, "wheel carrier" is a "carrier tool.

Package We strictly distinguish between internal perspectives and external perspectives with encapsuulation. An important advantage of packaging and replacement packages is possible to make the replacement (InterChangeability). When the system is divided into each component, the interaction between each part should be minimized. For example, the behavior of the transmitting mechanism encapsulates the behavior of the engine, allowing us to replace between different types of engines. Applying the above ideology to a software system in a software system, you need to discuss the task of software components that need to be executed, and how to complete this task separately.

Abstract Form: Class Category When the number of components in the system becomes large, the Catalogs are commonly used for organization. Different types of classifications are often used in daily life. For example, a phone number book, an Internet search engine, etc. Similarly, there are many categories in the software. For example, a list of methods, a list defined in a class, and the like.

Interfaces and implementations In software, we use "interface" to illustrate a task "What), use" Implementation "terms to illustrate this task" How to do "(how), That is, the external and internal viewing angles are explained, respectively. The interface interface illustrates what the design is dry. This is an abstract user needs to understand. The interface does not describe how the task is completed, so it can match all achievements that satisfy the abstraction. When developing a complex computer system, a key step is to divide the task into each component. In this way, the part can be developed by different teams. Each component has two sides, an interface to the outside world, and the implementation of the interface needs. Divide the interface and implementation, not only from the high-level angle to the design, but also makes the replacement of the software components possible. Every member of the group provides a service that is used by other members of the team. No member can solve the problem independently. Only common, coordination work can only complete the overall task.

Abstract form: Combination combination is another powerful technology to construct complex structures by simple portions. The mind is in a small number of simple forms, based on some combination rules, build a new form. The key in the combination is that the initial form can be combined or a combination of new forms.

Abstract form: The specialized hierarchy is another way of processing complexity, which is an abstraction using a specialized level. Sometimes this is also called a classification method. For example, biological is divided into animals and plants, animals are divided into vertebrates and invertebrates, and vertebrate includes mammals, mammals are divided into cats, dogs ..., etc. Non-standard behavioral duck beast reminds us that there will always be an exception (non-standard behavior) object-oriented language, and there is also a mechanism to override information from the previous level.

Abstract form: Patterns When we encounter new problems, most people will look at the new problems in the old problems that have been resolved. The previous problem can be used as a model that solves the problem, and the new problem can be solved slightly. This is the idea of ​​software mode (PATTERN).

Chapter 4

The extension of the parent class attribute is in the programming language, inherits means that the data and behavior of the subclass is the extension of the parent class related properties (a bigger set). Subclasses not only have all the properties of the parent class, but also some new properties can also be defined.

On the other hand, since the subclass is a more special (or more stringent) form of the subclass, it is also a simplified version of the parent class to some extent. Expansion and simplification of this inheritance as an extension as a contraction, it is a powerful reason for object-oriented technology, and it will also be confused in normal deployments.

Inheritance is to transfer downwards, so a class can inherit a variety of properties from a plurality of supercles above it. If DOG is a subclass of Mammal, and Mammal is a subclass of Animal, then DOG Not only inherited Mammal's properties, but also inherited animal properties.

Subclasses can cover behavior from parent class inheritance

Alternative replaceability is a powerful software development technique for object-oriented programming. Alternative means: The type specified when the variable declaration does not have to consisten to the value of the value it housed. This is not allowed in traditional programming languages, but often appears in object-oriented programming languages. Alternative often occurs when using the interface.

Judging whether the replaceability is legally based on the use of inheritance from the existing class to build a new class, it is determined whether or not the replaceability is legally based on the following: Sub-class instance must have all data fields of the parent class. Sub-class instances must implement all the features defined by the parent class, at least inheritance (if there is no clear coverage), the subclass can define a new function, which is independent of the replaceability discussion. Examples of subclasses can simulate the behavior of the parent class, under similar conditions, use the subclass instance to replace the parent class instance, should not be distinctively.

The legality basis is not always properly considered all of the use of inheritance, the legitimacy basis is not always correct. Therefore, not all subclasses constructed by inheritance can replace the parent class.

Submaturing subtypes are used to describe the relationship between classes that can significantly meet the replaceability principles. If two conditions are met, the type B can be called the subtype of type A. 1: The instance of type B can be legally assigned to variables that are declared as type A. 2: When the variable of the A type uses the B type instance, it will not cause changes to behavior.

Subclass and subtypes specifically use inheritance to build new categories. In the Java language, you can identify it through the extends keyword. The relationship between subtypes is more abstract, and this relationship can only be relieved through the source program. In most examples, the subclass is also a subtype. However, some way can be used to build subclasses that are not subtypes, and you can use an interface to construct a subtype, which makes the relevant types There is no inheritance relationship. It is important to understand the similarities between these two concepts.

Inheritance form

Specialization inherits many cases, in order to use inheritance and subcatenation. In this form, new categories are a specific type of parent class, which can meet all the specifications of the parent class. It is always a child type that is created in this way and is clearly in line with the replaceable principles. Together with standardized inheritance, these two ways constitute the ideal way, and is also a good design that should pursue.

Specification inherits the standardization inheritance to ensure that the subclass and the parent class have a common interface, that is, all subclasses implement methods with the same method header. The parent class has both implemented methods, and there is also a method of defining method heads, leaving a child. Subclasses only achieve those methods defined in the parent class but have not been implemented. Subclasses do not redefine existing types, but to implement an unfinished abstract specification. That is, the parent class defines some operations, but does not implement it. Only subclasses can implement them. In this case, the parent class is sometimes referred to as an abstract specification. In Java, keyword Abstract ensures that a subclass must be constructed. The category that is declared as Abstract must be subcatenated, and this type of instance is not could be created with a New operator. In addition, the method can be declared as abstract, and all the abstraction methods in the class must be overwritten before creating an instance. Standardized inheritance can be identified in the following ways: the parent class only provides method heads, and does not implement specific behavior, and the specific behavior must be implemented in subclasses. Constructuring inherits a class can inherit almost all required features from its parent class, just changing some method names used as class interfaces, or a list of parameters in the modification method. This implementation is also feasible even if there is no abstraction concept between the new class and the parent class.

Extended inheritance If the subclass is just adding new behaviors into the parent class, it does not modify any attributes from the parent class inheritance, ie the extension inheritance. Since the functionality of the parent class is still available, it is not modified, so the extended inherit does not violate the replaceability principle, and the subclass built in this manner or subtype

Limit inheritance If the behavior of the subclass is less than the parent class, it is limit inheritance. As with extended inheritance, when the programmer builds a new class with an existing class as base class, there is often a limit inherit, but this base class should not be modified. Limit inheritance can be described as a technique: it first receives those methods of inheritance and then make them invalid. Since the restriction inherits violates the replaceability principle, the subclass of it creates is not a child type, so it should be as not available as possible.

Merger inheritance (multiple inheritance) can form new abstraction by combining two or more abstract characteristics. A class can inherit the ability from multiple parent classes called multiple inheritance

Summary of inheritance mode: Subclasses are a special example of the parent class; that is, subclasses are a subtype of the parent class. Standardization: The behavior defined in the parent class needs to be implemented in subclasses, while the parent class itself does not implement these behaviors. Construction: Subclasses utilize the behavior provided by the parent class, but is not the child type of the parent class. Extension: Subclasses add some new features, but there is no change in the inheritance. Limit: Subclasses limit the use of some methods from the parent class. Merger: Subclasses inherited from multiple parent classes.

Programming Activities for multiplayers can be constructed using reusable components to construct programs, and programming has become a joint effort of groups from a personal behavior. A programmer can be a newly abstract type developer, or a user of software systems developed by other programmers. We also often mention organizations composed of several objects, where a customer object issues a service to a service provider Section 6 Polymorphism, that is, the object has a variety of forms. In object-oriented programming languages, polymorphism is "a" relationship, message delivery mechanism, inheritance, replaceability, etc. One main advantage of object-oriented programming (OOP) method is to combine these concepts, thereby producing many code sharing and multiplexing technologies.

6.1 Type Polymorphism (Polymorphism) refers to a single function available for multiple different types of parameters. In pure polymorphism, there is only one function (code block), but there can be multiple explanations (different purposes). Another situation is a plurality of different functions (code blocks) use the same name, which is called overload, sometimes called a specific polymorphism. In both extreme forms, the override and delayed method (Deferred Methods) 6.2 polymorphisms are covered by methods, and the polymorphisms in object-oriented languages ​​are only through polymorphic variables and alternative. Realization of thinking. The polymorphic variable has a polygon; that is, it can accommodate different types of values. The polymorphic variable reflects the replaceability principle. In other words, although the polymorphic variable also has a desired type, it can accommodate any subtype value of the desired type. For dynamically binding languages ​​(such as SmallTalk), all variables may be polymorphic - any variable can accommodate any type of value. For static types of languages ​​(such as Java), the situation is slightly more complicated. In Java, the polymorphism is achieved by the difference between the variable's declaration type (static) and its actual value-accommodated value type (dynamic).

6.3 Overload If the same method name corresponds to multiple method, we say that this method name is overloaded. Note that override is an essential part of override, but both methods are different, and overload can appear alone without coverage. For overload, only the method name is polymorphic - it has a variety of forms. We can also imagine overload and coverage into another way - there is only one abstract function that receives a variety of different types of parameters, and the actually executed code is determined by the received parameters. The compiler can usually determine the method used at compile, then generate a set of code, which is an optimized 6.3.1 real life overloaded news in Chapter 1, there is only an overload Covered example. Example: In order to give a friend a surprise, I wanted to give him a bunch of flowers in his birthday. One way to send a sendflowersto message to the local flower merchants; another way is to send my wife a same message. . Flowers and my wife can understand this message, both can respond and achieve the same results. To some extent, I can also think that the SendFlowersTo method is a method of understanding the wife and flower merchants, but they use different algorithms to respond to my request. Special attention, in this example, there is no inheritance, wife and flower merchants are human (human) of course, and SendFlowersTo is not a common, such as my dentist, although he is also a person, but if I have to him Send this news, he will be confused. 6.3.2 Overload and Mandatory Example: To develop a class library, the class library indicates that there are many data structures that can be used to accommodate a set of elements (for example, collections, packages, dictionaries, arrays, priority queues, etc.) . A ADD method is defined in these data structures to insert new elements in the data structure. This situation-provides semantic actions with two fully independent functions for different data types - often appears in all programming languages, not just in object-oriented languages. Example: Overloaded plus operator " ". The interands generated by the compiler must have a great difference with the code generated by the floating point addition, but for programmers, these operations are the same, all of which are addition functions. In this example, it is also necessary to point out that overload is not everything you want. A semantic independent operation - mandatory (COERCION) and is often related to arithmetic operations. Force for converting a digital type into another digital type. If the mixed type arithmetic operation is allowed, the two numbers can be explained in many different ways: there is a possibility to provide four different functions, named: integer integer, integer implement, real integers and real numbers Real number. In this case, only overload but is not mandatory. Provide two different functions: integer integer, real real number. For integer real numbers and real numbers integers, integer can be enforced into real numbers. In this case, the overload and mandatory are combined together. Only one function is provided: real numbers real numbers. Before the operation, all parameters are forced to convert into real numbers. In this case, it is only forced and there is no overload. 6.3.3 Overloading of the independent class has two different reload. One is that the same method name appears in multiple classes without inheritance; the other is, in the same class, there are multiple methods to use the same name.

The overload does not mean that there is no such semantic similarity between the methods similar to being overloaded. They only have the same name. This overload-independent, mutually discharging method uses the same name - is not considered a bad style, generally not chaos. In fact, choose some short, meaningful names such as add, draw, etc., help to understand and correctly use object-oriented components. When you need to add a new element to the collection, call the add method than call the addnewelement method, or call the set_modul_addition_method method, easier, concise. All object-oriented languages ​​allow for the use of the same method name in an unrelated class. At this time, the message recipient class needs to distinguish the reserved method name. However, this does not mean that these methods can receive any type of parameters. Java's static type characteristics still require way to specify all parameter types. 6.3.4 Parameter Overloads Another Style Overload - In the same context environment, multiple processes (functions or methods) use the same name, different numbers and types of parameters, this weight The way is called Parametric OverLoading. This overload method is not just in the Java language, many functional languages, and some ordered languages ​​(such as ADA) use this overload mode. Parameter overload often appears in the constructor method

6.4 Covering in a class (usually abstract superclass) defines a universal method, this method is to inherit and use by subclasses. At least in one sub-class, a method of the same name is defined. This method hides the sub-class instance to call the general method (if a generic method is revised in the subclass, the method of the subclass replaces the call to the general method. ). Let's say that the second method is override the first method. Override usually transparent to class users. Moreover, it is different from the overload that the two covered relationships are semantically considered to be the same.

6.5 Abstract Method Declare a method of abstract or a deferred method. We declare it in the parent class, but we left a child to go to specific implementation. Interfaces are also considered to be a delay method. Both cases are seen as a general form of coverage. In both cases, the subclasses modify the behavior of the parent class. For abstract methods, its behavior is actually empty, it only plays a placeholder, and all defined behavior is provided by subclasses.

6.6 Many of the pure polymorphisms Many authors use a polymorphism (Polymorphism, or a pure polymorphism) term for a method to receive a variety of different types of parameters, and use overload to represent multiple methods used A name. This usage does not imply the object-oriented language. For example, in a LISP or ML language, it is easy to write a function that can operate any type of element. This function is polymorphic because it does not know the exact type of the parameters when the function is defined. Creating a multi-state function is a strongest technology for object-oriented programming. It allows code to be written on a higher abstract level, then cut the code as needed to accommodate specific situations. Typically, the programmer sends a further message to the recipient of this method to achieve a cut. These further messages are usually unrelated to the classes of the definition polymorphism, but to send the delay method defined in the low layer class.

o Synual system: Value is considered to have only one type, that is, all data items must have a unique type O multi-state system: a value has more than one type of ability. o Multimets Support Technology: O (1) Force: Avoid the rigidity of the singular language, providing a limited polymorphic form. The relationship between mapping (forced) between types must be predetermined. Such as INT type and float type, the result is a Float type. o (2) Overload: The type of parameter will be used to select a suitable function. The plus function can operate two integers or two real numbers, and the type of parameters will be used for suitable functions. o (3) Parameterization polymorphism: A function will work consistently in a range type. Realize the class template (or a class). o (4) contains polymorphism: The function defined on a parent class can operate any subtype. Inheritance relationship is used. O The first two is called a specific multi-state, there is no principle, and only the type of specific number is supported; the two will be called universal polymorphisms, and there is a principle (usually similar structure and semantics), working in an unlimited Type collection. Advantages of multi-state languages: flexibility, abstraction, behavioral sharing, code sharing.

转载请注明原文地址:https://www.9cbs.com/read-13493.html

New Post(0)