




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、1 . Introduction To Objects1.1 The progress of abstractionAll programming languages provide abstractions. It can be argued that the complexity of the problems youre able to solve is directly related to the kind and quality of abstraction. By “kind” I mean, “What is it that you are abstracting?” Asse
2、mbly language is a small abstraction of the underlying machine. Many so-called “imperative” languages that followed (such as FORTRAN, BASIC, and C) were abstractions of assembly language. These languages are big improvements over assembly language, but their primary abstraction still requires you to
3、 think in terms of the structure of the computer rather than the structure of the problem you are trying to solve. The programmer must establish the association between the machine model (in the “solution space,” which is the place where youre modeling that problem, such as a computer) and the model
4、 of the problem that is actually being solved (in the “problem space,” which is the place where the problem exists). The effort required to perform this mapping, and the fact that it is extrinsic to the programming language, produces programs that are difficult to write and expensive to maintain, an
5、d as a side effect created the entire “programming methods” industry. The alternative to modeling the machine is to model the problem youre trying to solve. Early languages such as LISP and APL chose particular views of the world (“All problems are ultimately lists” or “All problems are algorithmic,
6、” respectively). PROLOG casts all problems into chains of decisions. Languages have been created for constraint-based programming and for programming exclusively by manipulating graphical symbols. (The latter proved to be too restrictive.) Each of these approaches is a good solution to the particula
7、r class of problem theyre designed to solve, but when you step outside of that domain they become awkward. The object-oriented approach goes a step further by providing tools for the programmer to represent elements in the problem space. This representation is general enough that the programmer is n
8、ot constrained to any particular type of problem. We refer to the elements in the problem space and their representations in the solution space as “objects.” (You will also need other objects that dont have problem-space analogs.) The idea is that the program is allowed to adapt itself to the lingo
9、of the problem by adding new types of objects, so when you read the code describing the solution, youre reading words that also express the problem. This is a more flexible and powerful language abstraction than what weve had before. Thus, OOP allows you to describe the problem in terms of the probl
10、em, rather than in terms of the computer where the solution will run. Theres still a connection back to the computer: each object looks quite a bit like a little computerit has a state, and it has operations that you can ask it to perform. However, this doesnt seem like such a bad analogy to objects
11、 in the real worldthey all have characteristics and behaviors. Alan Kay summarized five basic characteristics of Smalltalk, the first successful object-oriented language and one of the languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming:
12、 1. Everything is an object. Think of an object as a fancy variable; it stores data, but you can “make requests” to that object, asking it to perform operations on itself. In theory, you can take any conceptual component in the problem youre trying to solve (dogs, buildings, services, etc.) and repr
13、esent it as an object in your program. 2. A program is a bunch of objects telling each other what to do by sending messages. To make a request of an object, you “send a message” to that object. More concretely, you can think of a message as a request to call a method that belongs to a particular obj
14、ect. 3. Each object has its own memory made up of other objects. Put another way, you create a new kind of object by making a package containing existing objects. Thus, you can build complexity into a program while hiding it behind the simplicity of objects. 4. Every object has a type. Using the par
15、lance, each object is an instance of a class, in which “class” is synonymous with “type.” The most important distinguishing characteristic of a class is “What messages can you send to it?” 5. All objects of a particular type can receive the same messages. This is actually a loaded statement, as you
16、will see later. Because an object of type “circle” is also an object of type “shape,” a circle is guaranteed to accept shape messages. This means you can write code that talks to shapes and automatically handle anything that fits the description of a shape. This substitutability is one of the powerf
17、ul concepts in OOP. Booch offers an even more succinct description of an object:An object has state, behavior and identity.This means that an object can have internal data (which gives it state), methods (to produce behavior), and each object can be uniquely distinguished from every other objectto p
18、ut this in a concrete sense, each object has a unique address in memory.1.2 An object has an interfaceAristotle was probably the first to begin a careful study of the concept of type; he spoke of “the class of fishes and the class of birds.” The idea that all objects, while being unique, are also pa
19、rt of a class of objects that have characteristics and behaviors in common was used directly in the first object-oriented language, Simula-67, with its fundamental keyword class that introduces a new type into a program. Simula, as its name implies, was created for developing simulations such as the
20、 classic “bank teller problem.” In this, you have a bunch of tellers, customers, accounts, transactions, and units of moneya lot of “objects.” Objects that are identical except for their state during a programs execution are grouped together into “classes of objects” and thats where the keyword clas
21、s came from. Creating abstract data types (classes) is a fundamental concept in object-oriented programming. Abstract data types work almost exactly like built-in types: You can create variables of a type (called objects or instances in object-oriented parlance) and manipulate those variables (calle
22、d sending messages or requests; you send a message and the object figures out what to do with it). The members (elements) of each class share some commonality: every account has a balance, every teller can accept a deposit, etc. At the same time, each member has its own state: each account has a dif
23、ferent balance, each teller has a name. Thus, the tellers, customers, accounts, transactions, etc., can each be represented with a unique entity in the computer program. This entity is the object, and each object belongs to a particular class that defines its characteristics and behaviors. So, altho
24、ugh what we really do in object-oriented programming is create new data types, virtually all object-oriented programming languages use the “class” keyword. When you see the word “type” think “class” and vice versa.Since a class describes a set of objects that have identical characteristics (data ele
25、ments) and behaviors (functionality), a class is really a data type because a floating point number, for example, also has a set of characteristics and behaviors. The difference is that a programmer defines a class to fit a problem rather than being forced to use an existing data type that was desig
26、ned to represent a unit of storage in a machine. You extend the programming language by adding new data types specific to your needs. The programming system welcomes the new classes and gives them all the care and type-checking that it gives to built-in types. The object-oriented approach is not lim
27、ited to building simulations. Whether or not you agree that any program is a simulation of the system youre designing, the use of OOP techniques can easily reduce a large set of problems to a simple solution. Once a class is established, you can make as many objects of that class as you like, and th
28、en manipulate those objects as if they are the elements that exist in the problem you are trying to solve. Indeed, one of the challenges of object-oriented programming is to create a one-to-one mapping between the elements in the problem space and objects in the solution space. But how do you get an
29、 object to do useful work for you? There must be a way to make a request of the object so that it will do something, such as complete a transaction, draw something on the screen, or turn on a switch. And each object can satisfy only certain requests. The requests you can make of an object are define
30、d by its interface, and the type is what determines the interface. A simple example might be a representation of a light bulb: Light On() Off()Light lt = new Light();lt.on();The interface establishes what requests you can make for a particular object. However, there must be code somewhere to satisfy
31、 that request. This, along with the hidden data, comprises the implementation. From a procedural programming standpoint, its not that complicated. A type has a method associated with each possible request, and when you make a particular request to an object, that method is called. This process is us
32、ually summarized by saying that you “send a message” (make a request) to an object, and the object figures out what to do with that message (it executes code). Here, the name of the type/class is Light, the name of this particular Light object is lt, and the requests that you can make of a Light obj
33、ect are to turn it on, turn it off, make it brighter, or make it dimmer. You create a Light object by defining a “reference” (lt) for that object and calling new to request a new object of that type. To send a message to the object, you state the name of the object and connect it to the message requ
34、est with a period (dot). From the standpoint of the user of a predefined class, thats pretty much all there is to programming with objects. The preceding diagram follows the format of the Unified Modeling Language (UML). Each class is represented by a box, with the type name in the top portion of th
35、e box, any data members that you care to describe in the middle portion of the box, and the methods (the functions that belong to this object, which receive any messages you send to that object) in the bottom portion of the box. Often, only the name of the class and the public methods are shown in U
36、ML design diagrams, so the middle portion is not shown. If youre interested only in the class name, then the bottom portion doesnt need to be shown, either1.3 An object provides services. While youre trying to develop or understand a program design, one of the best ways to think about objects is as
37、“service providers.” Your program itself will provide services to the user, and it will accomplish this by using the services offered by other objects. Your goal is to produce (or even better, locate in existing code libraries) a set of objects that provide the ideal services to solve your problem.
38、A way to start doing this is to ask “if I could magically pull them out of a hat, what objects would solve my problem right away?” For example, suppose you are creating a bookkeeping program. You might imagine some objects that contain pre-defined bookkeeping input screens, another set of objects th
39、at perform bookkeeping calculations, and an object that handles printing of checks and invoices on all different kinds of printers. Maybe some of these objects already exist, and for the ones that dont, what would they look like? What services would those objects provide, and what objects would they
40、 need to fulfill their obligations? If you keep doing this, you will eventually reach a point where you can say either “that object seems simple enough to sit down and write” or “Im sure that object must exist already.” This is a reasonable way to decompose a problem into a set of objects. Thinking
41、of an object as a service provider has an additional benefit: it helps to improve the cohesiveness of the object. High cohesion is a fundamental quality of software design: It means that the various aspects of a software component (such as an object, although this could also apply to a method or a l
42、ibrary of objects) “fit together” well. One problem people have when designing objects is cramming too much functionality into one object. For example, in your check printing module, you may decide you need an object that knows all about formatting and printing. Youll probably discover that this is
43、too much for one object, and that what you need is three or more objects. One object might be a catalog of all the possible check layouts, which can be queried for information about how to print a check. One object or set of objects could be a generic printing interface that knows all about differen
44、t kinds of printers (but nothing about bookkeepingthis one is a candidate for buying rather than writing yourself). And a third object could use the services of the other two to accomplish the task. Thus, each object has a cohesive set of services it offers. In a good object-oriented design, each ob
45、ject does one thing well, but doesnt try to do too much. As seen here, this not only allows the discovery of objects that might be purchased (the printer interface object), but it also produces the possibility of an object that might be reused somewhere else (the catalog of check layouts). Treating
46、objects as service providers is a great simplifying tool, and its very useful not only during the design process, but also when someone else is trying to understand your code or reuse an objectif they can see the value of the object based on what service it provides, it makes it much easier to fit i
47、t into the design. 1. 對象入門1.1 抽象的進步 所有編程語言的最終目的都是提供一種“抽象”方法。一種較有爭議的說法是:解決問題的復雜程度直接取決于抽象的種類及質量。這兒的“種類”是指準備對什么進行“抽象”?匯編語言是對基礎機器的少量抽象。后來的許多“命令式”語言(如FORTRAN,BASIC和C)是對匯編語言的一種抽象。與匯編語言相比,這些語言已有了長足的進步,但它們的抽象原理依然要求我們著重考慮計算機的結構,而非考慮問題本身的結構。在機器模型(位于“方案空間”)與實際解決的問題模型(位于“問題空間”)之間,程序員必須建立起一種聯系。這個過程要求人們付出較大的精力,而且
48、由于它脫離了編程語言本身的范圍,造成程序代碼很難編寫,而且要花較大的代價進行維護。由此造成的副作用便是一門完善的“編程方法”學科。 為機器建模的另一個方法是為要解決的問題制作模型。對一些早期語言來說,如LISP和APL,它們的做法是“從不同的角度觀察世界”“所有問題都歸納為列表”或“所有問題都歸納為算法”。PROLOG則將所有問題都歸納為決策鏈。對于這些語言,我們認為它們一部分是面向基于“強制”的編程,另一部分則是專為處理圖形符號設計的。每種方法都有自己特殊的用途,適合解決某一類的問題。但只要超出了它們力所能及的范圍,就會顯得非常笨拙。 面向對象的程序設計在此基礎上則跨出了一大步,程序員可利用
49、一些工具表達問題空間內的元素。由于這種表達非常普遍,所以不必受限于特定類型的問題。我們將問題空間中的元素以及它們在方案空間的表示物稱作“對象”(Object)。當然,還有一些在問題空間沒有對應體的其他對象。通過添加新的對象類型,程序可進行靈活的調整,以便與特定的問題配合。所以在閱讀方案的描述代碼時,會讀到對問題進行表達的話語。與我們以前見過的相比,這無疑是一種更加靈活、更加強大的語言抽象方法。總之,OOP允許我們根據問題來描述問題,而不是根據方案。然而,仍有一個聯系途徑回到計算機。每個對象都類似一臺小計算機;它們有自己的狀態,而且可要求它們進行特定的操作。與現實世界的“對象”或者“物體”相比,
50、編程“對象”與它們也存在共通的地方:它們都有自己的特征和行為。Alan Kay總結了Smalltalk的五大基本特征。這是第一種成功的面向對象程序設計語言,也是Java的基礎語言。通過這些特征,我們可理解“純粹”的面向對象程序設計方法是什么樣的。(1)所有東西都是對象。可將對象想象成一種新型變量;它保存著數據,但可要求它對自身進行操作。理論上講,可從要解決的問題身上提出所有概念性的組件,然后在程序中將其表達為一個對象。 (2) 程序是一大堆對象的組合;通過消息傳遞,各對象知道自己該做些什么。為了向對象發出請求,需向那個對象“發送一條消息”。更具體地講,可將消息想象為一個調用請求,它調用的是從屬
51、于目標對象的一個子例程或函數。 (3) 每個對象都有自己的存儲空間,可容納其他對象。或者說,通過封裝現有對象,可制作出新型對象。所以,盡管對象的概念非常簡單,但在程序中卻可達到任意高的復雜程度。 (4) 每個對象都有一種類型。根據語法,每個對象都是某個“類”的一個“實例”。其中,“類”(Class)是“類型”(Type)的同義詞。一個類最重要的特征就是“能將什么消息發給它?”。 (5) 同一類所有對象都能接收相同的消息。這實際是別有含義的一種說法,大家不久便能理解。由于類型為“圓”(Circle)的一個對象也屬于類型為“形狀”(Shape)的一個對象,所以一個圓完全能接收形狀消息。這意味著可讓
52、程序代碼統一指揮“形狀”,令其自動控制所有符合“形狀”描述的對象,其中自然包括“圓”。這一特性稱為對象的“可替換性”,是OOP最重要的概念之一。 一些語言設計者認為面向對象的程序設計本身并不足以方便解決所有形式的程序問題,提倡將不同的方法組合成“多形程序設計語言”。1.2 對象的接口 亞里士多德或許是認真研究“類型”概念的第一人,他曾談及“魚類和鳥類”的問題。在世界首例面向對象語言Simula-67中,第一次用到了這樣的一個概念:所有對象盡管各有特色都屬于某一系列對象的一部分,這些對象具有通用的特征和行為。在Simula-67中,首次用到了class這個關鍵字,它為程序引入了一個全新的類型(c
53、las和type通常可互換使用;注釋)。 :有些人進行了進一步的區分,他們強調“類型”決定了接口,而“類”是那個接口的一種特殊實現方式。 Simula是一個很好的例子。正如這個名字所暗示的,它的作用是“模擬”(Simulate)象“銀行出納員”這樣的經典問題。在這個例子里,我們有一系列出納員、客戶、帳號以及交易等。每類成員(元素)都具有一些通用的特征:每個帳號都有一定的余額;每名出納都能接收客戶的存款;等等。與此同時,每個成員都有自己的狀態;每個帳號都有不同的余額;每名出納都有一個名字。所以在計算機程序中,能用獨一無二的實體分別表示出納員、客戶、帳號以及交易。這個實體便是“對象”,而且每個對象都隸屬一個特定的“類”,那個類具有自己的通用特征與
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 計算流體力學SOD激波管
- 設備維修協議書范文
- 表里的生物教案
- 江蘇省鹽城市射陽中學2025屆高三下學期全真模擬(4)生物試卷(有答案)
- 財務會計實習心得(15篇)
- 表526班組安全技術交底表樣板
- 廣東省部分學校2024-2025學年高一下學期6月月考歷史試題
- 幼兒園《春天的秘密》教學課件
- 財務會計沙盤實訓心得體會5篇
- 民航地勤通 用服務培訓教學課件
- 清華強基化學試題
- 2024-2030年中國機器人關節模組行業市場競爭態勢及前景戰略研判報告
- 實驗室儀器設備等采購項目培訓方案
- 三江學院輔導員考試試題2024
- UASB+SBR處理果汁廢水設計說明書及圖紙
- 華圖教育:2024年國考面試白皮書
- 2024年海港區社區工作者招聘筆試沖刺題(帶答案解析)
- 國開2024春專科《高等數學基礎》形考任務1-4試題及答案
- T-JSIA 0002-2022 能源大數據數據目錄指南
- 2024高校院長述職報告
- 化工催化與催化反應工程
評論
0/150
提交評論