![B8設(shè)計(jì)論文(C )外文文獻(xiàn)中英文翻譯(Object)[1].doc_第1頁(yè)](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc01.gif)
![B8設(shè)計(jì)論文(C )外文文獻(xiàn)中英文翻譯(Object)[1].doc_第2頁(yè)](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc02.gif)
![B8設(shè)計(jì)論文(C )外文文獻(xiàn)中英文翻譯(Object)[1].doc_第3頁(yè)](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc03.gif)
![B8設(shè)計(jì)論文(C )外文文獻(xiàn)中英文翻譯(Object)[1].doc_第4頁(yè)](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc04.gif)
![B8設(shè)計(jì)論文(C )外文文獻(xiàn)中英文翻譯(Object)[1].doc_第5頁(yè)](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc05.gif)
已閱讀5頁(yè),還剩3頁(yè)未讀, 繼續(xù)免費(fèi)閱讀
版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
皮晨暉 軟件081班7023108043Object landscapes and lifetimesTechnically, OOP is just about abstract data typing, inheritance, and polymorphism, but other issues can be at least as important. The remainder of this section will cover these issues. One of the most important factors is the way objects are created and destroyed. Where is the data for an object and how is the lifetime of the object controlled? There are different philosophies at work here. C+ takes the approach that control of efficiency is the most important issue, so it gives the programmer a choice. For maximum run-time speed, the storage and lifetime can be determined while the program is being written, by placing the objects on the stack (these are sometimes called automatic or scoped variables) or in the static storage area. This places a priority on the speed of storage allocation and release, and control of these can be very valuable in some situations. However, you sacrifice flexibility because you must know the exact quantity, lifetime, and type of objects while youre writing the program. If you are trying to solve a more general problem such as computer-aided design, warehouse management, or air-traffic control, this is too restrictive. The second approach is to create objects dynamically in a pool of memory called the heap. In this approach, you dont know until run-time how many objects you need, what their lifetime is, or what their exact type is. Those are determined at the spur of the moment while the program is running. If you need a new object, you simply make it on the heap at the point that you need it. Because the storage is managed dynamically, at run-time, the amount of time required to allocate storage on the heap is significantly longer than the time to create storage on the stack. (Creating storage on the stack is often a single assembly instruction to move the stack pointer down, and another to move it back up.) The dynamic approach makes the generally logical assumption that objects tend to be complicated, so the extra overhead of finding storage and releasing that storage will not have an important impact on the creation of an object. In addition, the greater flexibility is essential to solve the general programming problem. Java uses the second approach, exclusively. Every time you want to create an object, you use the new keyword to build a dynamic instance of that object. Theres another issue, however, and thats the lifetime of an object. With languages that allow objects to be created on the stack, the compiler determines how long the object lasts and can automatically destroy it. However, if you create it on the heap the compiler has no knowledge of its lifetime. In a language like C+, you must determine programmatically when to destroy the object, which can lead to memory leaks if you dont do it correctly (and this is a common problem in C+ programs). Java provides a feature called a garbage collector that automatically discovers when an object is no longer in use and destroys it. A garbage collector is much more convenient because it reduces the number of issues that you must track and the code you must write. More important, the garbage collector provides a much higher level of insurance against the insidious problem of memory leaks (which has brought many a C+ project to its knees). The rest of this section looks at additional factors concerning object lifetimes and landscapes. 1. The singly rooted hierarchyOne of the issues in OOP that has become especially prominent since the introduction of C+ is whether all classes should ultimately be inherited from a single base class. In Java (as with virtually all other OOP languages) the answer is “yes” and the name of this ultimate base class is simply Object. It turns out that the benefits of the singly rooted hierarchy are many. All objects in a singly rooted hierarchy have an interface in common, so they are all ultimately the same type. The alternative (provided by C+) is that you dont know that everything is the same fundamental type. From a backward-compatibility standpoint this fits the model of C better and can be thought of as less restrictive, but when you want to do full-on object-oriented programming you must then build your own hierarchy to provide the same convenience thats built into other OOP languages. And in any new class library you acquire, some other incompatible interface will be used. It requires effort (and possibly multiple inheritance) to work the new interface into your design. Is the extra “flexibility” of C+ worth it? If you need itif you have a large investment in Cits quite valuable. If youre starting from scratch, other alternatives such as Java can often be more productive. All objects in a singly rooted hierarchy (such as Java provides) can be guaranteed to have certain functionality. You know you can perform certain basic operations on every object in your system. A singly rooted hierarchy, along with creating all objects on the heap, greatly simplifies argument passing (one of the more complex topics in C+). A singly rooted hierarchy makes it much easier to implement a garbage collector (which is conveniently built into Java). The necessary support can be installed in the base class, and the garbage collector can thus send the appropriate messages to every object in the system. Without a singly rooted hierarchy and a system to manipulate an object via a reference, it is difficult to implement a garbage collector. Since run-time type information is guaranteed to be in all objects, youll never end up with an object whose type you cannot determine. This is especially important with system level operations, such as exception handling, and to allow greater flexibility in programming. 2 .Collection libraries and support for easy collection useBecause a container is a tool that youll use frequently, it makes sense to have a library of containers that are built in a reusable fashion, so you can take one off the shelf Because a container is a tool that youll use frequently, it makes sense to have a library of containers that are built in a reusable fashion, so you can take one off the shelf and plug it into your program. Java provides such a library, which should satisfy most needs. Downcasting vs. templates/genericsTo make these containers reusable, they hold the one universal type in Java that was previously mentioned: Object. The singly rooted hierarchy means that everything is an Object, so a container that holds Objects can hold anything. This makes containers easy to reuse. To use such a container, you simply add object references to it, and later ask for them back. But, since the container holds only Objects, when you add your object reference into the container it is upcast to Object, thus losing its identity. When you fetch it back, you get an Object reference, and not a reference to the type that you put in. So how do you turn it back into something that has the useful interface of the object that you put into the container? Here, the cast is used again, but this time youre not casting up the inheritance hierarchy to a more general type, you cast down the hierarchy to a more specific type. This manner of casting is called downcasting. With upcasting, you know, for example, that a Circle is a type of Shape so its safe to upcast, but you dont know that an Object is necessarily a Circle or a Shape so its hardly safe to downcast unless you know thats what youre dealing with. Its not completely dangerous, however, because if you downcast to the wrong thing youll get a run-time error called an exception, which will be described shortly. When you fetch object references from a container, though, you must have some way to remember exactly what they are so you can perform a proper downcast. Downcasting and the run-time checks require extra time for the running program, and extra effort from the programmer. Wouldnt it make sense to somehow create the container so that it knows the types that it holds, eliminating the need for the downcast and a possible mistake? The solution is parameterized types, which are classes that the compiler can automatically customize to work with particular types. For example, with a parameterized container, the compiler could customize that container so that it would accept only Shapes and fetch only Shapes. Parameterized types are an important part of C+, partly because C+ has no singly rooted hierarchy. In C+, the keyword that implements parameterized types is “template.” Java currently has no parameterized types since it is possible for it to get byhowever awkwardlyusing the singly rooted hierarchy. However, a current proposal for parameterized types uses a syntax that is strikingly similar to C+ templates. 對(duì)象的創(chuàng)建和存在時(shí)間從技術(shù)角度說(shuō),OOP(面向?qū)ο蟪绦蛟O(shè)計(jì))只是涉及抽象的數(shù)據(jù)類型、繼承以及多形性,但另一些問(wèn)題也可能顯得非常重要。本節(jié)將就這些問(wèn)題進(jìn)行探討。最重要的問(wèn)題之一是對(duì)象的創(chuàng)建及破壞方式。對(duì)象需要的數(shù)據(jù)位于哪兒,如何控制對(duì)象的“存在時(shí)間”呢?針對(duì)這個(gè)問(wèn)題,解決的方案是各異其趣的。C+認(rèn)為程序的執(zhí)行效率是最重要的一個(gè)問(wèn)題,所以它允許程序員作出選擇。為獲得最快的運(yùn)行速度,存儲(chǔ)以及存在時(shí)間可在編寫程序時(shí)決定,只需將對(duì)象放置在堆棧(有時(shí)也叫作自動(dòng)或定域變量)或者靜態(tài)存儲(chǔ)區(qū)域即可。這樣便為存儲(chǔ)空間的分配和釋放提供了一個(gè)優(yōu)先級(jí)。某些情況下,這種優(yōu)先級(jí)的控制是非常有價(jià)值的。然而,我們同時(shí)也犧牲了靈活性,因?yàn)樵诰帉懗绦驎r(shí),必須知道對(duì)象的準(zhǔn)確的數(shù)量、存在時(shí)間、以及類型。如果要解決的是一個(gè)較常規(guī)的問(wèn)題,如計(jì)算機(jī)輔助設(shè)計(jì)、倉(cāng)儲(chǔ)管理或者空中交通控制,這一方法就顯得太局限了。第二個(gè)方法是在一個(gè)內(nèi)存池中動(dòng)態(tài)創(chuàng)建對(duì)象,該內(nèi)存池亦叫“堆”或者“內(nèi)存堆”。若采用這種方式,除非進(jìn)入運(yùn)行期,否則根本不知道到底需要多少個(gè)對(duì)象,也不知道它們的存在時(shí)間有多長(zhǎng),以及準(zhǔn)確的類型是什么。這些參數(shù)都在程序正式運(yùn)行時(shí)才決定的。若需一個(gè)新對(duì)象,只需在需要它的時(shí)候在內(nèi)存堆里簡(jiǎn)單地創(chuàng)建它即可。由于存儲(chǔ)空間的管理是運(yùn)行期間動(dòng)態(tài)進(jìn)行的,所以在內(nèi)存堆里分配存儲(chǔ)空間的時(shí)間比在堆棧里創(chuàng)建的時(shí)間長(zhǎng)得多(在堆棧里創(chuàng)建存儲(chǔ)空間一般只需要一個(gè)簡(jiǎn)單的指令,將堆棧指針向下或向下移動(dòng)即可)。由于動(dòng)態(tài)創(chuàng)建方法使對(duì)象本來(lái)就傾向于復(fù)雜,所以查找存儲(chǔ)空間以及釋放它所需的額外開(kāi)銷不會(huì)為對(duì)象的創(chuàng)建造成明顯的影響。除此以外,更大的靈活性對(duì)于常規(guī)編程問(wèn)題的解決是至關(guān)重要的。C+允許我們決定是在寫程序時(shí)創(chuàng)建對(duì)象,還是在運(yùn)行期間創(chuàng)建,這種控制方法更加靈活。大家或許認(rèn)為既然它如此靈活,那么無(wú)論如何都應(yīng)在內(nèi)存堆里創(chuàng)建對(duì)象,而不是在堆棧中創(chuàng)建。但還要考慮另外一個(gè)問(wèn)題,亦即對(duì)象的“存在時(shí)間”或者“生存時(shí)間”(Lifetime)。若在堆?;蛘哽o態(tài)存儲(chǔ)空間里創(chuàng)建一個(gè)對(duì)象,編譯器會(huì)判斷對(duì)象的持續(xù)時(shí)間有多長(zhǎng),到時(shí)會(huì)自動(dòng)“破壞”或者“清除”它。程序員可用兩種方法來(lái)破壞一個(gè)對(duì)象:用程序化的方式?jīng)Q定何時(shí)破壞對(duì)象,或者利用由運(yùn)行環(huán)境提供的一種“垃圾收集器”特性,自動(dòng)尋找那些不再使用的對(duì)象,并將其清除。當(dāng)然,垃圾收集器顯得方便得多,但要求所有應(yīng)用程序都必須容忍垃圾收集器的存在,并能默許隨垃圾收集帶來(lái)的額外開(kāi)銷。但這并不符合C+語(yǔ)言的設(shè)計(jì)宗旨,所以未能包括到C+里。但Java確實(shí)提供了一個(gè)垃圾收集器(Smalltalk也有這樣的設(shè)計(jì);盡管Delphi默認(rèn)為沒(méi)有垃圾收集器,但可選擇安裝;而C+亦可使用一些由其他公司開(kāi)發(fā)的垃圾收集產(chǎn)品)。本節(jié)剩下的部分將討論操縱對(duì)象時(shí)要考慮的另一些因素。1 單根結(jié)構(gòu)在面向?qū)ο蟮某绦蛟O(shè)計(jì)中,由于C+的引入而顯得尤為突出的一個(gè)問(wèn)題是:所有類最終是否都應(yīng)從單獨(dú)一個(gè)基礎(chǔ)類繼承。在Java中(與其他幾乎所有OOP語(yǔ)言一樣),對(duì)這個(gè)問(wèn)題的答案都是肯定的,而且這個(gè)終級(jí)基礎(chǔ)類的名字很簡(jiǎn)單,就是一個(gè)“Object”。這種“單根結(jié)構(gòu)”具有許多方面的優(yōu)點(diǎn)。單根結(jié)構(gòu)中的所有對(duì)象都有一個(gè)通用接口,所以它們最終都屬于相同的類型。另一種方案(就象C+那樣)是我們不能保證所有東西都屬于相同的基本類型。從向后兼容的角度看,這一方案可與C模型更好地配合,而且可以認(rèn)為它的限制更少一些。但假期我們想進(jìn)行純粹的面向?qū)ο缶幊?,那么必須?gòu)建自己的結(jié)構(gòu),以期獲得與內(nèi)建到其他OOP語(yǔ)言里的同樣的便利。需添加我們要用到的各種新類庫(kù),還要使用另一些不兼容的接口。理所當(dāng)然地,這也需要付出額外的精力使新接口與自己的設(shè)計(jì)方案配合(可能還需要多重繼承)。為得到C+額外的“靈活性”,付出這樣的代價(jià)值得嗎?當(dāng)然,如果真的需要如果早已是C專家,如果對(duì)C有難舍的情結(jié)那么就真的很值得。但假如你是一名新手,首次接觸這類設(shè)計(jì),象Java那樣的替換方案也許會(huì)更省事一些。單根結(jié)構(gòu)中的所有對(duì)象(比如所有Java對(duì)象)都可以保證擁有一些特定的功能。在自己的系統(tǒng)中,我們知道對(duì)每個(gè)對(duì)象都能進(jìn)行一些基本操作。一個(gè)單根結(jié)構(gòu),加上所有對(duì)象都在內(nèi)存堆中創(chuàng)建,可以極大簡(jiǎn)化參數(shù)的傳遞(這在C+里是一個(gè)復(fù)雜的概念)。利用單根結(jié)構(gòu),我們可以更方便地實(shí)現(xiàn)一個(gè)垃圾收集器。與此有關(guān)的必要支持可安裝于基礎(chǔ)類中,而垃圾收集器可將適當(dāng)?shù)南l(fā)給系統(tǒng)內(nèi)的任何對(duì)象。如果沒(méi)有這種單根結(jié)構(gòu),而且系統(tǒng)通過(guò)一個(gè)句柄來(lái)操縱對(duì)象,那么實(shí)現(xiàn)垃圾收集器的途徑會(huì)有很大的不同,而且會(huì)面臨許多障礙。由于運(yùn)行期的類型信息肯定存在于所有對(duì)象中,所以永遠(yuǎn)不會(huì)遇到判斷不出一個(gè)對(duì)象的類型的情況。這對(duì)系統(tǒng)級(jí)的操作來(lái)說(shuō)顯得特別重要,比如違例控制;而且也能在程序設(shè)計(jì)時(shí)獲得更大的靈活性。2 集合庫(kù)與方便使用集合由于集合
溫馨提示
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 街道車位出租協(xié)議書(shū)
- 2025年陜西省省屬事業(yè)單位公開(kāi)招聘工作人員筆試歷年典型考題及考點(diǎn)剖析附帶答案詳解
- 簽定代收收款協(xié)議書(shū)
- 行車設(shè)備轉(zhuǎn)讓協(xié)議書(shū)
- 2025中國(guó)建材集團(tuán)有限公司所屬企業(yè)招聘15人筆試參考題庫(kù)附帶答案詳解
- 股份合作建廠協(xié)議書(shū)
- 貴陽(yáng)車禍免責(zé)協(xié)議書(shū)
- 繼承房屋子女協(xié)議書(shū)
- 解碼設(shè)備轉(zhuǎn)讓協(xié)議書(shū)
- 股權(quán)終止轉(zhuǎn)讓協(xié)議書(shū)
- 統(tǒng)編版2024-2025第二學(xué)期小學(xué)六年級(jí)期末語(yǔ)文測(cè)試卷(有答案)
- 2025年物流管理專業(yè)考試試卷及答案
- 2025年全國(guó)保密教育線上培訓(xùn)考試試題庫(kù)及參考答案【鞏固】含答案詳解
- 中藥學(xué)三基題庫(kù)
- 關(guān)鍵設(shè)備管理與維護(hù)策略
- 中華人民共和國(guó)民營(yíng)經(jīng)濟(jì)促進(jìn)法
- 臨床類面試真題及答案
- 夫妻間借款協(xié)議合同
- 【8地一模 初二會(huì)考】2025年安徽省亳州市利辛縣中考一模地理試題(含解析)
- ktv服務(wù)員合同協(xié)議書(shū)范本
- 2025年中國(guó)心電電極片市場(chǎng)調(diào)查研究報(bào)告
評(píng)論
0/150
提交評(píng)論