计算机专业毕业设计论文外文文献中英文翻译.doc
《计算机专业毕业设计论文外文文献中英文翻译.doc》由会员分享,可在线阅读,更多相关《计算机专业毕业设计论文外文文献中英文翻译.doc(15页珍藏版)》请在沃文网上搜索。
1、Object landscapes and lifetimes Technically, OOP is just about abstract data type, 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
2、 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 d
3、etermined when 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 situatio
4、ns. 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
5、second approach is to create objects dynamically in a pool of memory called the heap. In this approach, you dont know how many objects you need, what their lifetime is, or what their exact type is until run-time. Those are determined at the spur of the moment while the program is running. If you nee
6、d 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 o
7、ften 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 o
8、n the creation of an object. In addition, the greater flexibility is essential to solve the general programming problem. C+ 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, howeve
9、r, 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 mu
10、st 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 ga
11、rbage collector is much more convenient because it reduces the number of issues that you must track and the code you must write. The rest of this section looks at additional factors concerning object landscapes and lifetimes.1 Collections and iteratorsIf you dont know how many objects youre going to
12、 need to solve a particular problem, or how long they will last, you also dont know how to store those objects. How can you know how much space to create for those objects? You cant, since that information isnt known until run-time. The solution to most problems in object-oriented design seems flipp
13、ant: you create another type of object. The new type of object that solves this particular problem holds references to other objects. Of course, you can do the same thing with an array, which is available in most languages. But theres more. This new object, generally called a container (also called
14、a collection, but the Java library uses that term in a different sense so this book will use “container”), will expand itself whenever necessary to accommodate everything you place inside it. So you dont need to know how many objects youre going to hold in a container. Just create a container object
15、 and let it take care of the details. Fortunately, a good OOP language comes with a set of containers as part of the package. In C+, its part of the Standard C+ Library and is sometimes called the Standard Template Library (STL). Object Pascal has containers in its Visual Component Library (VCL). Sm
16、alltalk has a very complete set of containers. Java also has containers in its standard library. In some libraries, a generic container is considered good enough for all needs, and in others (Java, for example) the library has different types of containers for different needs: a vector (called an Ar
17、rayList in Java) for consistent access to all elements, and a linked list for consistent insertion at all elements, for example, so you can choose the particular type that fits your needs. Container libraries may also include sets, queues, hash tables, trees, stacks, etc. All containers have some wa
18、y to put things in and get things out; there are usually functions to add elements to a container, and others to fetch those elements back out. But fetching elements can be more problematic, because a single-selection function is restrictive. What if you want to manipulate or compare a set of elemen
19、ts in the container instead of just one? The solution is an iterator, which is an object whose job is to select the elements within a container and present them to the user of the iterator. As a class, it also provides a level of abstraction. This abstraction can be used to separate the details of t
20、he container from the code thats accessing that container. The container, via the iterator, is abstracted to be simply a sequence. The iterator allows you to traverse that sequence without worrying about the underlying structurethat is, whether its an ArrayList, a LinkedList, a Stack, or something e
21、lse. This gives you the flexibility to easily change the underlying data structure without disturbing the code in your program. Java began (in version 1.0 and 1.1) with a standard iterator, called Enumeration, for all of its container classes. Java 2 has added a much more complete container library
22、that contains an iterator called Iterator that does more than the older Enumeration. From a design standpoint, all you really want is a sequence that can be manipulated to solve your problem. If a single type of sequence satisfied all of your needs, thered be no reason to have different kinds. There
23、 are two reasons that you need a choice of containers. First, containers provide different types of interfaces and external behavior. A stack has a different interface and behavior than that of a queue, which is different from that of a set or a list. One of these might provide a more flexible solut
24、ion to your problem than the other. Second, different containers have different efficiencies for certain operations. The best example is an ArrayList and a LinkedList. Both are simple sequences that can have identical interfaces and external behaviors. But certain operations can have radically diffe
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
20 积分
下载 | 加入VIP,下载更划算! |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 计算机专业 毕业设计 论文 外文 文献 中英文 翻译