版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、畢業(yè)設(shè)計(jì)外文資料翻譯學(xué) 院: 專(zhuān)業(yè)班級(jí): 學(xué)生姓名: 學(xué) 號(hào): 指導(dǎo)教師: 外文出處: Thinking in Java 4th Edition 附 件:1.外文資料翻譯譯文; 2.外文原文 指導(dǎo)教師評(píng)語(yǔ):該英文文獻(xiàn)是介紹Java對(duì)象基本概念的資料,與畢業(yè)設(shè)計(jì)所用的技術(shù)相關(guān)度高。從該生翻譯的內(nèi)容來(lái)看,術(shù)語(yǔ)翻譯準(zhǔn)確度比較高,體現(xiàn)了較強(qiáng)的英語(yǔ)應(yīng)用水平。翻譯工作認(rèn)真細(xì)致,嚴(yán)格按照規(guī)定,翻譯材料能與原文能保持一致,能正確表達(dá)出原文意思。簽名: 年 月 日外文資料翻譯譯文Thinking in Java 4th Edition對(duì)象入門(mén) “我們自然地減少并將它組織成概念,并賦予意義,很大程度上是因?yàn)樵谖覀?/p>
2、的語(yǔ)言模式下各方達(dá)成一種協(xié)議除了通過(guò)訂閱的組織和分類(lèi)數(shù)據(jù)協(xié)議的法令,否則我們不能交流?!北窘苊骼钗譅柗?1897 - 1941)計(jì)算機(jī)革命的起源是一臺(tái)機(jī)器。因此我們的編程語(yǔ)言的起源往往也是這樣的機(jī)器。但電腦并不全是機(jī)器思維放大工具(史蒂夫喬布斯喜歡說(shuō)成“頭腦的自行車(chē)”)和不同的表達(dá)媒介。結(jié)果,工具開(kāi)始看起來(lái)不那么像機(jī)器,更像我們的思想,也像 其他形式的表達(dá),如寫(xiě)作、繪畫(huà)、雕塑、動(dòng)畫(huà)和電影制作。面向?qū)ο缶幊?OOP)是一部分用計(jì)算機(jī)作為表達(dá)媒介的活動(dòng)。本章將介紹OOP的基本概念,包括開(kāi)發(fā)方法的概述。這一章乃至這本書(shū),假設(shè)你有編程經(jīng)驗(yàn),盡管不一定在C。如果你覺(jué)得你需要在開(kāi)始閱讀這本書(shū)之前要在編程上
3、有更多的準(zhǔn)備,你應(yīng)該先看看在C多媒體研討會(huì)中思考,它能從www.MindV下載。這一章是背景和補(bǔ)充材料。許多人在不了解大局的情況下涉水面向?qū)ο缶幊滩粫?huì)覺(jué)得太容易。因此,這里有許多概念來(lái)給你介紹一個(gè)完備的OOP的概述。然而,其他人可能不會(huì)理解全局概念直到先給他們看的一些結(jié)構(gòu),這些人可能會(huì)變得停滯不前并且失去了一些代碼來(lái)讓他們上手。如果你是后者的一部分并渴望得到具體的語(yǔ)言,隨意跳過(guò)這章節(jié)-跳過(guò)它不會(huì)阻止你寫(xiě)程序或?qū)W習(xí)語(yǔ)言。然而,你將最終要回到這里來(lái)填滿你的知識(shí)這樣就你就能理解為什么對(duì)象很重要并且如何來(lái)設(shè)計(jì)它們。1.1 抽象的進(jìn)步 所有編程語(yǔ)言的最終目的都是提供一種“抽象”方法。一種較有爭(zhēng)議的說(shuō)法是
4、:解決問(wèn)題的復(fù)雜程度直接取 決于抽象的種類(lèi)及質(zhì)量。這兒的“種類(lèi)”是指準(zhǔn)備對(duì)什么進(jìn)行“抽象”?匯編語(yǔ)言是對(duì)基礎(chǔ)機(jī)器的少量抽 象。后來(lái)的許多“命令式”語(yǔ)言(如FORTRAN,BASIC 和 C)是對(duì)匯編語(yǔ)言的一種抽象。與匯編語(yǔ)言相比,這 些語(yǔ)言已有了長(zhǎng)足的進(jìn)步,但它們的抽象原理依然要求我們著重考慮計(jì)算機(jī)的結(jié)構(gòu),而非考慮問(wèn)題本身的結(jié) 構(gòu)。在機(jī)器模型(位于“方案空間”)與實(shí)際解決的問(wèn)題模型(位于“問(wèn)題空間”)之間,程序員必須建立起一種聯(lián)系。這個(gè)過(guò)程要求人們付出較大的精力,而且由于它脫離了編程語(yǔ)言本身的范圍,造成程序代碼很 難編寫(xiě),而且要花較大的代價(jià)進(jìn)行維護(hù)。由此造成的副作用便是一門(mén)完善的“編程方法”學(xué)
5、科。 為機(jī)器建模的另一個(gè)方法是為要解決的問(wèn)題制作模型。對(duì)一些早期語(yǔ)言來(lái)說(shuō),如 LISP 和 APL,它們的做法是 “從不同的角度觀察世界”“所有問(wèn)題都?xì)w納為列表”或“所有問(wèn)題都?xì)w納為算法”。PROLOG則將所有 問(wèn)題都?xì)w納為決策鏈。對(duì)于這些語(yǔ)言,我們認(rèn)為它們一部分是面向基于“強(qiáng)制”的編程,另一部分則是專(zhuān)為 處理圖形符號(hào)設(shè)計(jì)的。每種方法都有自己特殊的用途,適合解決某一類(lèi)的問(wèn)題。但只要超出了它們力所能及的范圍,就會(huì)顯得非常笨拙。 面向?qū)ο蟮某绦蛟O(shè)計(jì)在此基礎(chǔ)上則跨出了一大步,程序員可利用一些工具表達(dá)問(wèn)題空間內(nèi)的元素。由于這種表達(dá)非常普遍,所以不必受限于特定類(lèi)型的問(wèn)題。我們將問(wèn)題空間中的元素以及它們?cè)?/p>
6、方案空間的表示物稱(chēng) 作“對(duì)象”(Object)。當(dāng)然,還有一些在問(wèn)題空間沒(méi)有對(duì)應(yīng)體的其他對(duì)象。通過(guò)添加新的對(duì)象類(lèi)型,程序可進(jìn)行靈活的調(diào)整,以便與特定的問(wèn)題配合。所以在閱讀方案的描述代碼時(shí),會(huì)讀到對(duì)問(wèn)題進(jìn)行表達(dá)的話語(yǔ)。與我們以前見(jiàn)過(guò)的相比,這無(wú)疑是一種更加靈活、更加強(qiáng)大的語(yǔ)言抽象方法??傊?,OOP允許我們根據(jù)問(wèn)題來(lái)描述問(wèn)題,而不是根據(jù)方案。然而,仍有一個(gè)聯(lián)系途徑回到計(jì)算機(jī)。每個(gè)對(duì)象都類(lèi)似一臺(tái)小計(jì)算機(jī); 它們有自己的狀態(tài),而且可要求它們進(jìn)行特定的操作。與現(xiàn)實(shí)世界的“對(duì)象”或者“物體”相比,編程“對(duì)象”與它們也存在共通的地方:它們都有自己的特征和行為。 Alan Kay 總結(jié)了 Smalltalk
7、的五大基本特征。這是第一種成功的面向?qū)ο蟪绦蛟O(shè)計(jì)語(yǔ)言,也是Java 的基礎(chǔ) 語(yǔ)言。通過(guò)這些特征,我們可理解“純粹”的面向?qū)ο蟪绦蛟O(shè)計(jì)方法是什么樣的: (1)所有東西都是對(duì)象。可將對(duì)象想象成一種新型變量;它保存著數(shù)據(jù),但可要求它對(duì)自身進(jìn)行操作。理論 上講,可從要解決的問(wèn)題身上提出所有概念性的組件,然后在程序中將其表達(dá)為一個(gè)對(duì)象。 (2) 程序是一大堆對(duì)象的組合;通過(guò)消息傳遞,各對(duì)象知道自己該做些什么。為了向?qū)ο蟀l(fā)出請(qǐng)求,需向那個(gè)對(duì)象“發(fā)送一條消息”。更具體地講,可將消息想象為一個(gè)調(diào)用請(qǐng)求,它調(diào)用的是從屬于目標(biāo)對(duì)象的一個(gè) 子例程或函數(shù)。(3) 每個(gè)對(duì)象都有自己的存儲(chǔ)空間,可容納其他對(duì)象?;蛘哒f(shuō),通
8、過(guò)封裝現(xiàn)有對(duì)象,可制作出新型對(duì)象。所 以,盡管對(duì)象的概念非常簡(jiǎn)單,但在程序中卻可達(dá)到任意高的復(fù)雜程度。 (4) 每個(gè)對(duì)象都有一種類(lèi)型。根據(jù)語(yǔ)法,每個(gè)對(duì)象都是某個(gè)“類(lèi)”的一個(gè)“實(shí)例”。其中,“類(lèi)”(Class) 是“類(lèi)型”(Type)的同義詞。一個(gè)類(lèi)最重要的特征就是“能將什么消息發(fā)給它?”。 (5) 同一類(lèi)所有對(duì)象都能接收相同的消息。這實(shí)際是別有含義的一種說(shuō)法,大家不久便能理解。由于類(lèi)型為 “圓”(Circle)的一個(gè)對(duì)象也屬于類(lèi)型為“形狀”(Shape)的一個(gè)對(duì)象,所以一個(gè)圓完全能接收形狀消 息。這意味著可讓程序代碼統(tǒng)一指揮“形狀”,令其自動(dòng)控制所有符合“形狀”描述的對(duì)象,其中自然包括 “圓”
9、。這一特性稱(chēng)為對(duì)象的“可替換性”,是OOP最重要的概念之一。 一些語(yǔ)言設(shè)計(jì)者認(rèn)為面向?qū)ο蟮某绦蛟O(shè)計(jì)本身并不足以方便解決所有形式的程序問(wèn)題,提倡將不同的方法組 合成“多形程序設(shè)計(jì)語(yǔ)言”。 對(duì)象具有狀態(tài),行為和身份。這意味著一個(gè)對(duì)象可以有內(nèi)部數(shù)據(jù)(給它狀態(tài))、方法(生產(chǎn)行為),和每個(gè)對(duì)象可以獨(dú)特區(qū)別于其他對(duì)象把這放個(gè)在一個(gè)具體的容器中,在內(nèi)存每個(gè)對(duì)象都有一個(gè)唯一的地址。 1.2 對(duì)象的接口 亞里士多德或許是認(rèn)真研究“類(lèi)型”概念的第一人,他曾談及“魚(yú)類(lèi)和鳥(niǎo)類(lèi)”的問(wèn)題。在世界首例面向?qū)ο?語(yǔ)言Simula-67 中,第一次用到了這樣的一個(gè)概念: 所有對(duì)象盡管各有特色都屬于某一系列對(duì)象的一部分,這些對(duì)象
10、具有通用的特征和行為。在 Simula-67 中,首次用到了class 這個(gè)關(guān)鍵字,它為程序引入了一個(gè)全新的類(lèi)型。(clas 和 type 通常可互換使 用;注釋?zhuān)?:有些人進(jìn)行了進(jìn)一步的區(qū)分,他們強(qiáng)調(diào)“類(lèi)型”決定了接口,而“類(lèi)”是那個(gè)接口的一種特殊實(shí)現(xiàn)方式。Simula是一個(gè)很好的例子。正如這個(gè)名字所暗示的,它的作用是“模擬”(Simulate)象“銀行出納員”這 樣的經(jīng)典問(wèn)題。在這個(gè)例子里,我們有一系列出納員、客戶、帳號(hào)以及交易等。每類(lèi)成員(元素)都具有一 些通用的特征:每個(gè)帳號(hào)都有一定的余額;每名出納都能接收客戶的存款;等等。與此同時(shí),每個(gè)成員都有 自己的狀態(tài);每個(gè)帳號(hào)都有不同的余額;
11、每名出納都有一個(gè)名字。所以在計(jì)算機(jī)程序中,能用獨(dú)一無(wú)二的實(shí) 體分別表示出納員、客戶、帳號(hào)以及交易。這個(gè)實(shí)體便是“對(duì)象”,而且每個(gè)對(duì)象都隸屬一個(gè)特定的 “類(lèi)”,那個(gè)類(lèi)具有自己的通用特征與行為。 因此,在面向?qū)ο蟮某绦蛟O(shè)計(jì)中,盡管我們真正要做的是新建各種各樣的數(shù)據(jù)“類(lèi)型”(Type),但幾乎所有面向?qū)ο蟮某绦蛟O(shè)計(jì)語(yǔ)言都采用了“class”關(guān)鍵字。當(dāng)您看到“type”這個(gè)字的時(shí)候,請(qǐng)同時(shí)想到 “class”;反之亦然。 由于一個(gè)類(lèi)描述了一組具有相同特征的對(duì)象(數(shù)據(jù)元素)和行為(功能),因?yàn)橐粋€(gè)浮點(diǎn)數(shù),一個(gè)類(lèi)可是一個(gè)數(shù)據(jù)類(lèi)型,舉個(gè)例子,它也有一系列的特征和行為。所不同的是, 一個(gè)程序員定義一個(gè)類(lèi)來(lái)適應(yīng)
12、一個(gè)問(wèn)題而不是被迫使用現(xiàn)有的用來(lái)代表一個(gè)單位的存儲(chǔ)在一臺(tái)機(jī)器的數(shù)據(jù)類(lèi)型。你通過(guò)添加新的數(shù)據(jù)類(lèi)型擴(kuò)展編程語(yǔ)言達(dá)到你的特定需要。編程系統(tǒng)歡迎新類(lèi)并且給他們所有的照顧和類(lèi)型檢查這樣可以給內(nèi)置類(lèi)型。面向?qū)ο蟮姆椒ú⒉痪窒抻趧?chuàng)建模擬。無(wú)論你是否同意任何程序是一個(gè)模擬的系統(tǒng)設(shè)計(jì)、使用OOP技術(shù)可以很容易地大量的問(wèn)題減少成一個(gè)簡(jiǎn)單的解決方案。建好一個(gè)類(lèi)后,可根據(jù)情況生成許多對(duì)象。隨后,可將那些對(duì)象作為要解決問(wèn)題中存在的元素進(jìn)行處理。事實(shí)上,當(dāng)我們進(jìn)行面向?qū)ο蟮某绦蛟O(shè)計(jì)時(shí),面臨的最大一項(xiàng)挑戰(zhàn)性就是:如何在“問(wèn)題空間”(問(wèn)題實(shí)際存 在的地方)的元素與“方案空間”(對(duì)實(shí)際問(wèn)題進(jìn)行建模的地方,如計(jì)算機(jī))的元素之間建
13、立理想的“一對(duì)一”對(duì)應(yīng)或映射關(guān)系。 如何利用對(duì)象完成真正有用的工作呢?必須有一種辦法能向?qū)ο蟀l(fā)出請(qǐng)求,令其做一些實(shí)際的事情,比如完 成一次交易、在屏幕上畫(huà)一些東西或者打開(kāi)一個(gè)開(kāi)關(guān)等等。每個(gè)對(duì)象僅能接受特定的請(qǐng)求。我們向?qū)ο蟀l(fā)出 的請(qǐng)求是通過(guò)它的“接口”(Interface)定義的,對(duì)象的“類(lèi)型”或“類(lèi)”則規(guī)定了它的接口形式?!邦?lèi)型”與“接口”的等價(jià)或?qū)?yīng)關(guān)系是面向?qū)ο蟪绦蛟O(shè)計(jì)的基礎(chǔ)。 下面讓我們以電燈泡為例: 接口確定您可以針對(duì)特定對(duì)象的請(qǐng)求。然而,必須在有代碼的地方才可以滿足這一要求。這包括實(shí)施與隱藏的數(shù)據(jù)。從程序設(shè)計(jì)的角度來(lái)看,這并不復(fù)雜。類(lèi)型有一個(gè)與每個(gè)可能的請(qǐng)求相關(guān)聯(lián)的方法,當(dāng)您對(duì)某個(gè)
14、對(duì)象做了一個(gè)特定的請(qǐng)求時(shí),該方法被調(diào)用。這個(gè)過(guò)程通常是通過(guò)說(shuō)你“發(fā)送消息”(向一個(gè)對(duì)象發(fā)送一個(gè)請(qǐng)求)來(lái)概括,并且該對(duì)象將處理該消息(它執(zhí)行的代碼)。在這個(gè)例子中,類(lèi)型類(lèi)的名稱(chēng)是 Light,可向 Light 對(duì)象發(fā)出的請(qǐng)求包括包括打開(kāi)(on)、關(guān)閉(off)、 變得更明亮(brighten)或者變得更暗淡(dim)。通過(guò)簡(jiǎn)單地聲明一個(gè)名字(lt),我們?yōu)?Light 對(duì)象創(chuàng)建 了一個(gè)“句柄”。然后用new關(guān)鍵字新建類(lèi)型為 Light 的一個(gè)對(duì)象。再用等號(hào)將其賦給句柄。為了向?qū)ο蟀l(fā) 送一條消息,我們列出句柄名(lt),再用一個(gè)句點(diǎn)符號(hào)(.)把它同消息名稱(chēng)(on)連接起來(lái)。從中可以看出,使用一些預(yù)
15、先定義好的類(lèi)時(shí),我們?cè)诔绦蚶锊捎玫拇a是非常簡(jiǎn)單和直觀的。 前面的圖是統(tǒng)一建模語(yǔ)言(UML)的格式。每一個(gè)類(lèi)代表一個(gè)盒子而且在盒子的類(lèi)型名稱(chēng)在頂部,任何你關(guān)心的數(shù)據(jù)成員會(huì)在中間部分的描述,并且方法(屬于這個(gè)對(duì)象的功能,收到任何消息,你發(fā)送到該對(duì)象)在底部的部分。通常,只有類(lèi)的公共方法的名稱(chēng)是UML設(shè)計(jì)圖所顯示的,所以中間的部分是不顯示的,在這種情況下。如果你只對(duì)類(lèi)名稱(chēng)感興趣,那么下面的部分就不需要顯示了。 HYPERLINK /?aldtype=85&keyfrom=alading l # 1.3對(duì)象提供服務(wù)當(dāng)你試圖開(kāi)發(fā)或理解一個(gè)程序設(shè)計(jì)時(shí),最好的方法之一是把對(duì)象當(dāng)作“服務(wù)提供者”,你的程序本
16、身將為用戶提供服務(wù),并且它將使用其他對(duì)象提供的服務(wù)來(lái)實(shí)現(xiàn)這個(gè)目標(biāo)。你可以看Introduction to Objects 19 中提及的(甚至更好地定位在現(xiàn)有的代碼庫(kù))一組對(duì)象解決問(wèn)題提供最佳服務(wù)。開(kāi)始做這件事的一個(gè)方法是問(wèn),“如果我能神奇地把他們從一頂帽子里拉出來(lái),什么東西會(huì)馬上解決我的問(wèn)題?例如,假設(shè)你正在創(chuàng)建一個(gè)記賬程序。你可能會(huì)想到一些對(duì)象包含預(yù)先定義的記賬式輸入屏幕,另一組的對(duì)象執(zhí)行的記賬計(jì)算,并且還有一個(gè)對(duì)象處理印刷的檢查和發(fā)票的所有不同類(lèi)型的打印機(jī)。也許有些對(duì)象已經(jīng)存在,對(duì)于那些不喜歡的對(duì)象,他們會(huì)是什么樣子?這些對(duì)象提供什么服務(wù),他們需要什么樣的目標(biāo)來(lái)完成他們的義務(wù)?如果你繼續(xù)
17、這樣做,你最終會(huì)達(dá)到一個(gè)點(diǎn),你可以說(shuō),“這個(gè)對(duì)象似乎很簡(jiǎn)單,坐下來(lái)寫(xiě)”或“我確信,對(duì)象必須存在”這是一個(gè)合理的方式來(lái)分解成一組問(wèn)題的對(duì)象。思想的對(duì)象作為服務(wù)供應(yīng)商有一個(gè)額外的好處:它有助于提高對(duì)象的凝聚力。高內(nèi)聚性是軟件設(shè)計(jì)的一個(gè)基本品質(zhì):它意味著軟件組件的各個(gè)方面(例如一個(gè)對(duì)象,雖然這也適用于一個(gè)方法或一個(gè)對(duì)象庫(kù))“適合”。一個(gè)問(wèn)題就是在設(shè)計(jì)對(duì)象時(shí)就已經(jīng)把太多的功能放到一個(gè)對(duì)象。例如,在您的支票打印模塊,您可以決定您需要一個(gè)對(duì)象,知道所有的格式化和打印。你可能會(huì)發(fā)現(xiàn),這對(duì)于一個(gè)對(duì)象來(lái)說(shuō)太多了,你需要的是三個(gè)或更多的對(duì)象。一個(gè)對(duì)象可以是一個(gè)所有檢查布局的目錄,它可以查詢(xún)有關(guān)如何打印檢查的信息。
18、一個(gè)對(duì)象或一組對(duì)象可以是一個(gè)通用的打印接口,知道所有的不同類(lèi)型的打印機(jī)(但沒(méi)有什么關(guān)于記帳這一個(gè)是購(gòu)買(mǎi)而不是寫(xiě)自己的候選人)。三個(gè)對(duì)象之中的任何一個(gè)對(duì)象可以使用其他的服務(wù)來(lái)完成任務(wù)。因此,每一個(gè)對(duì)象都有一個(gè)集合的服務(wù)提供。在一個(gè)良好的面向?qū)ο蟮脑O(shè)計(jì)中,每一個(gè)對(duì)象做一件事,但不會(huì)試圖做太多。這不僅允許現(xiàn)有的對(duì)象可以能夠購(gòu)買(mǎi)(打印機(jī)接口對(duì)象),但它也產(chǎn)生新的對(duì)象,可能會(huì)再利用其他地方(檢查布局的目錄)。把對(duì)象作為服務(wù)提供者是一個(gè)很好的簡(jiǎn)化工具。這不僅是在設(shè)計(jì)過(guò)程中游泳,而且在當(dāng)別人試圖理解你的代碼或重用一個(gè)對(duì)象時(shí)也有用。如果他們能看到這個(gè)對(duì)象的價(jià)值,根據(jù)它提供的服務(wù),它可以更容易地把它自己融入到設(shè)
19、計(jì)中去。 HYPERLINK /?aldtype=85&keyfrom=alading l # 1.4 HYPERLINK /?aldtype=85&keyfrom=alading l # o 清空 隱藏的實(shí)現(xiàn) 為方便后面的討論,讓我們先對(duì)這一領(lǐng)域的從業(yè)人員作一下分類(lèi)。從根本上說(shuō),大致有兩方面的人員涉足面向?qū)ο蟮木幊蹋骸邦?lèi)創(chuàng)建者”(創(chuàng)建新數(shù)據(jù)類(lèi)型的人)以及“客戶程序員”(在自己的應(yīng)用程序中采用現(xiàn)成數(shù)據(jù)類(lèi)型的人)。 對(duì)客戶程序員來(lái)講,最主要的目標(biāo)就是收集一個(gè)充斥著各種類(lèi)的編程“工具箱”,以便快速開(kāi)發(fā)符合自己要求的應(yīng)用。而對(duì)類(lèi)創(chuàng)建者來(lái)說(shuō),他們的目標(biāo)則是從頭構(gòu)建一個(gè)類(lèi),只向客戶程序員開(kāi)放有必要開(kāi)放的
20、東西(接口),其他所有細(xì)節(jié)都隱藏起來(lái)。為什么要這樣做?隱藏之后,客戶程序員就不能接觸和改變那些細(xì)節(jié),所以原創(chuàng)者不用擔(dān)心自己的作品會(huì)受到非法修改,可確保它們不會(huì)對(duì)其他人造成影響?!敖涌凇保?Interface)規(guī)定了可對(duì)一個(gè)特定的對(duì)象發(fā)出哪些請(qǐng)求。然而,必須在某個(gè)地方存在著一些代碼,以便滿足這些請(qǐng)求。這些代碼與那些隱藏起來(lái)的數(shù)據(jù)便叫作“隱藏的實(shí)現(xiàn)”。站在程式化程序編寫(xiě)( Procedural Programming )的角度,整個(gè)問(wèn)題并不顯得復(fù)雜。一種類(lèi)型含有與每種可能的請(qǐng)求關(guān)聯(lián)起來(lái)的函數(shù)。一旦向?qū)ο蟀l(fā)出一個(gè)特定的請(qǐng)求,就會(huì)調(diào)用那個(gè)函數(shù)。我們通常將這個(gè)過(guò)程總結(jié)為向?qū)ο蟆鞍l(fā)送一條消息”(提出一個(gè)請(qǐng)
21、求)。對(duì)象的職責(zé)就是決定如何對(duì)這條消息作出反應(yīng)(執(zhí)行相應(yīng)的代碼)。對(duì)于任何關(guān)系,重要一點(diǎn)是讓牽連到的所有成員都遵守相同的規(guī)則。創(chuàng)建一個(gè)庫(kù)時(shí),相當(dāng)于同客戶程序員建立了一種關(guān)系。 對(duì)方也是程序員,但他們的目標(biāo)是組合出一個(gè)特定的應(yīng)用(程序),或者用您的庫(kù)構(gòu)建一個(gè)更大的庫(kù)。若任何人都能使用一個(gè)類(lèi)的所有成員,那么客戶程序員可對(duì)那個(gè)類(lèi)做任何事情,沒(méi)有辦法強(qiáng)制他們遵守任何約束。即便非常不愿客戶程序員直接操作類(lèi)內(nèi)包含的一些成員,但倘若未進(jìn)行訪問(wèn)控制,就沒(méi)有辦法阻止這一情況的發(fā)生 所有東西都會(huì)暴露無(wú)遺。有兩方面的原因促使我們控制對(duì)成員的訪問(wèn)。第一個(gè)原因是防止程序員接觸他們不該接觸的東西 通常是內(nèi)部數(shù)據(jù)類(lèi)型的設(shè)計(jì)
22、思想。若只是為了解決特定的問(wèn)題,用戶只需操作接口即可,毋需明白這些信息。我們向用戶提供的實(shí)際是一種服務(wù),因?yàn)樗麄兒苋菀拙涂煽闯瞿男?duì)自己非常重要,以及哪些可忽略不計(jì)。進(jìn)行訪問(wèn)控制的第二個(gè)原因是允許庫(kù)設(shè)計(jì)人員修改內(nèi)部結(jié)構(gòu),不用擔(dān)心它會(huì)對(duì)客戶程序員造成什么影響。例如,我們最開(kāi)始可能設(shè)計(jì)了一個(gè)形式簡(jiǎn)單的類(lèi),以便簡(jiǎn)化開(kāi)發(fā)。以后又決定進(jìn)行改寫(xiě),使其更快地運(yùn)行。若接口與實(shí)現(xiàn)方法早已隔離開(kāi),并分別受到保護(hù),就可放心做到這一點(diǎn),只要求用戶重新鏈接一下即可。Java 采用三個(gè)顯式(明確)關(guān)鍵字以及一個(gè)隱式(暗示)關(guān)鍵字來(lái)設(shè)置類(lèi)邊界: public,private,protected 以及暗示性的 friendl
23、y。若未明確指定其他關(guān)鍵字,則默認(rèn)為后者。這些關(guān)鍵字的使用和含義都是相當(dāng)直觀的,它們決定了誰(shuí)能使用后續(xù)的定義內(nèi)容?!皃ublic”(公共)意味著后續(xù)的定義任何人均可使用。而在另一方面,“private”(私有)意味著除您自己、類(lèi)型的創(chuàng)建者以及那個(gè)類(lèi)型的內(nèi)部函數(shù)成員,其他任何人都不能訪問(wèn)后續(xù)的定義信息。 private 在您與客戶程序員之間豎起了一堵墻。若有人試圖訪問(wèn)私有成員,就會(huì)得到一個(gè)編譯期錯(cuò)誤?!癴riendly ”(友好的)涉及“包裝”或“封裝”(Package)的概念即 Java 用來(lái)構(gòu)建庫(kù)的方法。若某樣?xùn)|西是“友好的”,意味著它只能在這個(gè)包裝的范圍內(nèi)使用(所以這一訪問(wèn)級(jí)別有時(shí)也叫作“
24、包裝訪問(wèn)”)?!?protected”(受保護(hù)的)與“ private”相似,只是一個(gè)繼承的類(lèi)可訪問(wèn)受保護(hù)的成員,但不能訪問(wèn)私有成員。繼承的問(wèn)題不久就要談到。2.外文原文Introduction to Objects“We cut nature up, organize it into concepts, and ascribe significances as we do, largely because we are parties to an agreement that holds throughout our speech community and is codified in t
25、he patterns of our language we cannot talk at all except by subscribing to the organization and classification of data which the agreement decrees.”Benjamin Lee Whorf (1897-1941)The genesis of the computer revolution was in a machine. The genesis of our programming languages thus tends to look like
26、that machine. But computers are not so much machines as they are mind amplification tools (“bicycles for the mind,” as Steve Jobs is fond of saying) and a different kind of expressive medium. As a result, the tools are beginning to look less like machines and more like parts of our minds, and also l
27、ike other forms of expression such as writing, painting, sculpture, animation, and filmmaking. Object-oriented programming (OOP) is part of this movement toward using the computer as an expressive medium.This chapter will introduce you to the basic concepts of OOP, including an overview of developme
28、nt methods. This chapter, and this book, assumes that you have someprogramming experience, although not necessarily in C. If you think you need morepreparation in programming before tackling this book, you should work through theThinking in C multimedia seminar, downloadable from HYPERLINK http:/www
29、.MindV. www.MindV.This chapter is background and supplementary material. Many people do not feel comfortable wading into object-oriented programming without understanding the big picture first. Thus, there are many concepts that are introduced here to give you a solid overview of OOP. However, other
30、 people may not get the big picture concepts until theyve seen some of the mechanics first; these people may become bogged down and lost without some code to get their hands on. If youre part of this latter group and are eager to get to the specifics of the language, feel free to jump past this chap
31、terskipping it at this point will not prevent you from writing programs or learning the language. However, you will want to come back here eventually to fill in your knowledge so you can understand why objects are important and how to design with them.The progress of abstractionAll programming langu
32、ages 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?” Assembly language is a small abstraction of the underlying machine. Many so-called
33、“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 think in terms of the structure of the computer rather than the structure of t
34、he problem you are trying to solve. The programmer must establish the association between the machinemodel (in the “solution space,” which is the place where youre implementing that solution, such as a computer) and the model of the problem that is actually being solved (in the 16 Thinking in Java B
35、ruce Eckel “problem space,” which is the place where the problem exists, such as a business). 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, and as a side effect crea
36、ted 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,” respectively). Prolog
37、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 may be a good solution to the particular class of problem t
38、heyre 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 not constrained to any
39、 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 of the problem by add
40、ing 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.1 Thus, OOP allows you to describe the problem in terms of the problem, rather than in t
41、erms 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 hasoperations that you can ask it to perform. However, this doesnt seem like such a bad analogy to objects in the real worldthe
42、y all have characteristics and behaviors.Alan Kay summarized five basic characteristics of Smalltalk, the first successful objectoriented language and one of the languages upon which Java is based. These characteristics represent a pure approach to object-oriented programming:1. Everything is an obj
43、ect. 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 represent it as an object in
44、 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 object.3. Each object has it
45、s 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 parlance, each object is an in
46、stance 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 will see later. Because an o
47、bject 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 thatfits the description of a shape. This substitutability is one of the powerful concepts in OOP.Booch offe
48、rs 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 put this in a concrete sense, e
49、ach object has a unique address in memory.2An 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 part of a class of objects that hav
50、e 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 classic “bank teller problem.” In
51、 this, you have numerous 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 class came from. Creating abstract data
52、 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 (called sending messages or requests; you
53、 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 different balance, each teller has a n
54、ame. 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, although what we really do in object-orie
55、nted 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.3Since a class describes a set of objects that have identical characteristics (data elements) and behaviors (functionality
56、), 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 designed to represent a unit of storage
57、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 limited to building simulations. Whethe
58、r 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 then manipulate those objects as if the
59、y 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-toone mapping between the elements in the problem space and objects in the solution space. But how do you get an object to do useful work for you? The
60、re needs to 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 defined by its interface, and the type i
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五年度職工住房借款與社區(qū)養(yǎng)老配套服務(wù)協(xié)議3篇
- 二零二五年度吊裝工程安全評(píng)估與監(jiān)督管理合同2篇
- 2025年辦公家具回收利用與環(huán)保處理合同3篇
- 極簡(jiǎn)市場(chǎng)營(yíng)銷(xiāo)(杰瑞20231224)
- 二零二五年度花椒采摘與旅游融合發(fā)展合同3篇
- 2024年陽(yáng)泉煤業(yè)(集團(tuán))有限責(zé)任公司總醫(yī)院三礦醫(yī)院高層次衛(wèi)技人才招聘筆試歷年參考題庫(kù)頻考點(diǎn)附帶答案
- 2024年河南水利與環(huán)境職業(yè)學(xué)院高職單招數(shù)學(xué)歷年參考題庫(kù)含答案解析
- 2024年河北機(jī)電職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試歷年參考題庫(kù)含答案解析
- 二零二五年度美食街?jǐn)偽蛔赓U及廣告投放合同3篇
- 2024年江蘇財(cái)經(jīng)職業(yè)技術(shù)學(xué)院高職單招職業(yè)適應(yīng)性測(cè)試歷年參考題庫(kù)含答案解析
- 2024年石家莊正定國(guó)際機(jī)場(chǎng)改擴(kuò)建工程合同
- 2025年度愛(ài)讀書(shū)學(xué)長(zhǎng)定制化閱讀計(jì)劃合同2篇
- 河南省信陽(yáng)市浉河區(qū)9校聯(lián)考2024-2025學(xué)年八年級(jí)上學(xué)期12月月考地理試題(含答案)
- 快速康復(fù)在骨科護(hù)理中的應(yīng)用
- 國(guó)民經(jīng)濟(jì)行業(yè)分類(lèi)和代碼表(電子版)
- ICU患者外出檢查的護(hù)理
- 公司收購(gòu)設(shè)備合同范例
- 廣東省潮州市2023-2024學(xué)年高二上學(xué)期語(yǔ)文期末考試試卷(含答案)
- 2024年光伏發(fā)電項(xiàng)目EPC總包合同
- 試卷(完整版)python考試復(fù)習(xí)題庫(kù)復(fù)習(xí)知識(shí)點(diǎn)試卷試題
- GB/T 44679-2024叉車(chē)禁用與報(bào)廢技術(shù)規(guī)范
評(píng)論
0/150
提交評(píng)論