![B8設計論文(C )外文文獻中英文翻譯(Object)[1].doc_第1頁](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc01.gif)
![B8設計論文(C )外文文獻中英文翻譯(Object)[1].doc_第2頁](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc02.gif)
![B8設計論文(C )外文文獻中英文翻譯(Object)[1].doc_第3頁](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc03.gif)
![B8設計論文(C )外文文獻中英文翻譯(Object)[1].doc_第4頁](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc04.gif)
![B8設計論文(C )外文文獻中英文翻譯(Object)[1].doc_第5頁](http://file1.renrendoc.com/fileroot_temp2/2020-3/10/6aa9cefd-c60f-46f4-a576-eae267687cc0/6aa9cefd-c60f-46f4-a576-eae267687cc05.gif)
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
皮晨暉 軟件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. 對象的創(chuàng)建和存在時間從技術角度說,OOP(面向?qū)ο蟪绦蛟O計)只是涉及抽象的數(shù)據(jù)類型、繼承以及多形性,但另一些問題也可能顯得非常重要。本節(jié)將就這些問題進行探討。最重要的問題之一是對象的創(chuàng)建及破壞方式。對象需要的數(shù)據(jù)位于哪兒,如何控制對象的“存在時間”呢?針對這個問題,解決的方案是各異其趣的。C+認為程序的執(zhí)行效率是最重要的一個問題,所以它允許程序員作出選擇。為獲得最快的運行速度,存儲以及存在時間可在編寫程序時決定,只需將對象放置在堆棧(有時也叫作自動或定域變量)或者靜態(tài)存儲區(qū)域即可。這樣便為存儲空間的分配和釋放提供了一個優(yōu)先級。某些情況下,這種優(yōu)先級的控制是非常有價值的。然而,我們同時也犧牲了靈活性,因為在編寫程序時,必須知道對象的準確的數(shù)量、存在時間、以及類型。如果要解決的是一個較常規(guī)的問題,如計算機輔助設計、倉儲管理或者空中交通控制,這一方法就顯得太局限了。第二個方法是在一個內(nèi)存池中動態(tài)創(chuàng)建對象,該內(nèi)存池亦叫“堆”或者“內(nèi)存堆”。若采用這種方式,除非進入運行期,否則根本不知道到底需要多少個對象,也不知道它們的存在時間有多長,以及準確的類型是什么。這些參數(shù)都在程序正式運行時才決定的。若需一個新對象,只需在需要它的時候在內(nèi)存堆里簡單地創(chuàng)建它即可。由于存儲空間的管理是運行期間動態(tài)進行的,所以在內(nèi)存堆里分配存儲空間的時間比在堆棧里創(chuàng)建的時間長得多(在堆棧里創(chuàng)建存儲空間一般只需要一個簡單的指令,將堆棧指針向下或向下移動即可)。由于動態(tài)創(chuàng)建方法使對象本來就傾向于復雜,所以查找存儲空間以及釋放它所需的額外開銷不會為對象的創(chuàng)建造成明顯的影響。除此以外,更大的靈活性對于常規(guī)編程問題的解決是至關重要的。C+允許我們決定是在寫程序時創(chuàng)建對象,還是在運行期間創(chuàng)建,這種控制方法更加靈活。大家或許認為既然它如此靈活,那么無論如何都應在內(nèi)存堆里創(chuàng)建對象,而不是在堆棧中創(chuàng)建。但還要考慮另外一個問題,亦即對象的“存在時間”或者“生存時間”(Lifetime)。若在堆?;蛘哽o態(tài)存儲空間里創(chuàng)建一個對象,編譯器會判斷對象的持續(xù)時間有多長,到時會自動“破壞”或者“清除”它。程序員可用兩種方法來破壞一個對象:用程序化的方式?jīng)Q定何時破壞對象,或者利用由運行環(huán)境提供的一種“垃圾收集器”特性,自動尋找那些不再使用的對象,并將其清除。當然,垃圾收集器顯得方便得多,但要求所有應用程序都必須容忍垃圾收集器的存在,并能默許隨垃圾收集帶來的額外開銷。但這并不符合C+語言的設計宗旨,所以未能包括到C+里。但Java確實提供了一個垃圾收集器(Smalltalk也有這樣的設計;盡管Delphi默認為沒有垃圾收集器,但可選擇安裝;而C+亦可使用一些由其他公司開發(fā)的垃圾收集產(chǎn)品)。本節(jié)剩下的部分將討論操縱對象時要考慮的另一些因素。1 單根結構在面向?qū)ο蟮某绦蛟O計中,由于C+的引入而顯得尤為突出的一個問題是:所有類最終是否都應從單獨一個基礎類繼承。在Java中(與其他幾乎所有OOP語言一樣),對這個問題的答案都是肯定的,而且這個終級基礎類的名字很簡單,就是一個“Object”。這種“單根結構”具有許多方面的優(yōu)點。單根結構中的所有對象都有一個通用接口,所以它們最終都屬于相同的類型。另一種方案(就象C+那樣)是我們不能保證所有東西都屬于相同的基本類型。從向后兼容的角度看,這一方案可與C模型更好地配合,而且可以認為它的限制更少一些。但假期我們想進行純粹的面向?qū)ο缶幊蹋敲幢仨殬嫿ㄗ约旱慕Y構,以期獲得與內(nèi)建到其他OOP語言里的同樣的便利。需添加我們要用到的各種新類庫,還要使用另一些不兼容的接口。理所當然地,這也需要付出額外的精力使新接口與自己的設計方案配合(可能還需要多重繼承)。為得到C+額外的“靈活性”,付出這樣的代價值得嗎?當然,如果真的需要如果早已是C專家,如果對C有難舍的情結那么就真的很值得。但假如你是一名新手,首次接觸這類設計,象Java那樣的替換方案也許會更省事一些。單根結構中的所有對象(比如所有Java對象)都可以保證擁有一些特定的功能。在自己的系統(tǒng)中,我們知道對每個對象都能進行一些基本操作。一個單根結構,加上所有對象都在內(nèi)存堆中創(chuàng)建,可以極大簡化參數(shù)的傳遞(這在C+里是一個復雜的概念)。利用單根結構,我們可以更方便地實現(xiàn)一個垃圾收集器。與此有關的必要支持可安裝于基礎類中,而垃圾收集器可將適當?shù)南l(fā)給系統(tǒng)內(nèi)的任何對象。如果沒有這種單根結構,而且系統(tǒng)通過一個句柄來操縱對象,那么實現(xiàn)垃圾收集器的途徑會有很大的不同,而且會面臨許多障礙。由于運行期的類型信息肯定存在于所有對象中,所以永遠不會遇到判斷不出一個對象的類型的情況。這對系統(tǒng)級的操作來說顯得特別重要,比如違例控制;而且也能在程序設計時獲得更大的靈活性。2 集合庫與方便使用集合由于集合
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年刮墨刀項目資金申請報告代可行性研究報告
- 2025年度教育科技股權分配及資源共享協(xié)議模板
- 2025年度事業(yè)單位聘用合同書模板(保密協(xié)議)正式版
- 2025年度保密性產(chǎn)品研發(fā)與生產(chǎn)合作協(xié)議
- 2025年河南中醫(yī)藥大學單招職業(yè)技能測試題庫及答案一套
- 2025年農(nóng)村集體土地租賃與使用權轉(zhuǎn)讓協(xié)議
- 2025年度宅基地使用權流轉(zhuǎn)備案與監(jiān)管服務合同
- 二零二五年度電影演員跨界合作合同范本
- 咖啡廳垃圾運輸合作協(xié)議
- 2025年度新能源產(chǎn)業(yè)研發(fā)人工費合作協(xié)議
- 私立醫(yī)療機構2025年運營策略與計劃
- 四川省眉山市眉山中學2024-2025學年高二上學期11月期中考試試題2
- 2025年蘇州農(nóng)業(yè)職業(yè)技術學院高職單招高職單招英語2016-2024歷年頻考點試題含答案解析
- 公共服務均等化研究-第2篇-深度研究
- 字體設計完整版本
- 短視頻居間代理合同范本
- 二零二五年度港口碼頭安全承包服務協(xié)議4篇
- 2024年蘇州衛(wèi)生職業(yè)技術學院高職單招語文歷年參考題庫含答案解析
- 《歡樂運動會:1 我為班級出把力》說課稿-2024-2025學年四年級上冊綜合實踐活動滬科黔科版
- 2025年中智集團及下屬單位招聘筆試參考題庫含答案解析
- 廣東2025年高中化學學業(yè)水平考試模擬試卷試題(含答案詳解)
評論
0/150
提交評論