計算機畢業(yè)論文中英對照外文翻譯——對象的創(chuàng)建和存在時間.doc
約13頁DOC格式手機打開展開
計算機畢業(yè)論文中英對照外文翻譯——對象的創(chuàng)建和存在時間,technically, oop is just about abstract data typing, inheritance, and polymorphism, but other issues can be at least as important. the remainder of this section...
內容介紹
此文檔由會員 wanli1988go 發(fā)布
Technically, 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 you're 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 don't 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
從技術角度說,OOP(面向對象程序設計)只是涉及抽象的數據類型、繼承以及多形性,但另一些問題也可能顯得非常重要。本節(jié)將就這些問題進行探討。
最重要的問題之一是對象的創(chuàng)建及破壞方式。對象需要的數據位于哪兒,如何控制對象的“存在時間”呢?針對這個問題,解決的方案是各異其趣的。C++認為程序的執(zhí)行效率是最重要的一個問題,所以它允許程序員作出選擇。為獲得最快的運行速度,存儲以及存在時間可在編寫程序時決定,只需將對象放置在堆棧(有時也叫作自動或定域變量)或者靜態(tài)存儲區(qū)域即可。這樣便為存儲空間的分配和釋放提供了一個優(yōu)先級。某些情況下,這種優(yōu)先級的控制是非常有價值的。然而,我們同時也犧牲了靈活性,因為在編寫程序時,必須知道對象的準確的數量、存在時間、以及類型。如果要解決的是一個較常規(guī)的問題,如計算機輔助設計、倉儲管理或者空中交通控制,這一方法就顯得太局限了。
第二個方法是在一個內存池中動態(tài)創(chuàng)建對象,該內存池亦叫“堆”或者“內存堆”。若采用這種方式,除非進入運行期,否則根本不知道到底需要多少個對象,也不知道它們的存在時間有多長,以及準確的類型是什么。這些參數都在程序正式運行時才決定的。若需一個新對象,只需在需要它的時候在內存堆里簡單地創(chuàng)建它即可。由于存儲空間的管理是運行期間動態(tài)進行的,所以在內存堆里分配存儲空間的時間比在堆棧里創(chuàng)建的時間長得多(在堆棧里創(chuàng)建存儲空間一般只需要一個簡單的指令,將堆棧指針向下或向下移動即可)。由于動態(tài)創(chuàng)建方法使對象本來就傾向于復雜,所以查找存儲空間以及釋放它所需的額外開銷不會為對象的創(chuàng)建造成明顯的影響。除此以外,更大的靈活性對于常規(guī)編程問題的解決是至關重要的。
C++允許我們決定是在寫程序時創(chuàng)建對象,還是在運行期間創(chuàng)建,這種控制方法更加靈活。大家或許認為既然它如此靈活,那么無論如何都應在內存堆里創(chuàng)建對象,而不是在堆棧中創(chuàng)建。
但還要考慮另外一個問題,亦即對象的“存在時間”或者“生存時間”(Lifetime)。若在堆?;蛘哽o態(tài)存儲空間里創(chuàng)建一個對象,編譯器會判斷對象的持續(xù)時間有多長,到時會自動“破壞”或者“清除”它。程序員可用兩種方法來破壞一個對象:用程序化的方式決定何時破壞對象,或者利用由運行環(huán)境提供的一種“垃圾收集器”特性,自動尋找那些不再使用的對象,并將其清除。當然,垃圾收集器顯得方便得多,但要求所有應用程序都必須容忍垃圾收集器的存在,并能默許隨垃圾收集帶來的額外開銷。但這并不符合C++語言的設計宗旨,所以未能包括到C++里。但Java確實提供了一個垃圾收集器(Smalltalk也有這樣的設計;盡管Delphi默認為沒有垃圾收集器,但可選擇安裝;而C++亦可使用一些由其他公司開發(fā)的垃圾收集產品)。
本節(jié)剩下的部分將討論操縱對象時要考慮的另一些因素。
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 you're 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 don't 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
從技術角度說,OOP(面向對象程序設計)只是涉及抽象的數據類型、繼承以及多形性,但另一些問題也可能顯得非常重要。本節(jié)將就這些問題進行探討。
最重要的問題之一是對象的創(chuàng)建及破壞方式。對象需要的數據位于哪兒,如何控制對象的“存在時間”呢?針對這個問題,解決的方案是各異其趣的。C++認為程序的執(zhí)行效率是最重要的一個問題,所以它允許程序員作出選擇。為獲得最快的運行速度,存儲以及存在時間可在編寫程序時決定,只需將對象放置在堆棧(有時也叫作自動或定域變量)或者靜態(tài)存儲區(qū)域即可。這樣便為存儲空間的分配和釋放提供了一個優(yōu)先級。某些情況下,這種優(yōu)先級的控制是非常有價值的。然而,我們同時也犧牲了靈活性,因為在編寫程序時,必須知道對象的準確的數量、存在時間、以及類型。如果要解決的是一個較常規(guī)的問題,如計算機輔助設計、倉儲管理或者空中交通控制,這一方法就顯得太局限了。
第二個方法是在一個內存池中動態(tài)創(chuàng)建對象,該內存池亦叫“堆”或者“內存堆”。若采用這種方式,除非進入運行期,否則根本不知道到底需要多少個對象,也不知道它們的存在時間有多長,以及準確的類型是什么。這些參數都在程序正式運行時才決定的。若需一個新對象,只需在需要它的時候在內存堆里簡單地創(chuàng)建它即可。由于存儲空間的管理是運行期間動態(tài)進行的,所以在內存堆里分配存儲空間的時間比在堆棧里創(chuàng)建的時間長得多(在堆棧里創(chuàng)建存儲空間一般只需要一個簡單的指令,將堆棧指針向下或向下移動即可)。由于動態(tài)創(chuàng)建方法使對象本來就傾向于復雜,所以查找存儲空間以及釋放它所需的額外開銷不會為對象的創(chuàng)建造成明顯的影響。除此以外,更大的靈活性對于常規(guī)編程問題的解決是至關重要的。
C++允許我們決定是在寫程序時創(chuàng)建對象,還是在運行期間創(chuàng)建,這種控制方法更加靈活。大家或許認為既然它如此靈活,那么無論如何都應在內存堆里創(chuàng)建對象,而不是在堆棧中創(chuàng)建。
但還要考慮另外一個問題,亦即對象的“存在時間”或者“生存時間”(Lifetime)。若在堆?;蛘哽o態(tài)存儲空間里創(chuàng)建一個對象,編譯器會判斷對象的持續(xù)時間有多長,到時會自動“破壞”或者“清除”它。程序員可用兩種方法來破壞一個對象:用程序化的方式決定何時破壞對象,或者利用由運行環(huán)境提供的一種“垃圾收集器”特性,自動尋找那些不再使用的對象,并將其清除。當然,垃圾收集器顯得方便得多,但要求所有應用程序都必須容忍垃圾收集器的存在,并能默許隨垃圾收集帶來的額外開銷。但這并不符合C++語言的設計宗旨,所以未能包括到C++里。但Java確實提供了一個垃圾收集器(Smalltalk也有這樣的設計;盡管Delphi默認為沒有垃圾收集器,但可選擇安裝;而C++亦可使用一些由其他公司開發(fā)的垃圾收集產品)。
本節(jié)剩下的部分將討論操縱對象時要考慮的另一些因素。