【正文】
The solution to most problems in objectoriented design seems flippant: 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 there’s more. This new object, generally called a container (also called 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 acmodate everything you place inside it. So you don’t need to know how manyobjects you’re going to hold in a container. Just create a container object and let it take care of the details. Fortunately, a good OOP language es with a set of containers as part of the package. In C++, it’s 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). Smalltalk has a very plete 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 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 way to put things in and get things out。t know until runtime 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 runtime, 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 plicated, 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. There39。 6 英文原文 Object landscapes and lifetimes 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 runtime 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 you39。在 C++中,用于實(shí)現(xiàn)參數(shù)化類型的關(guān)鍵字是 template( 模板)。例如,通過(guò)使用一個(gè)參數(shù)化集合,編譯器可對(duì)那個(gè)集合進(jìn)行定制,使其只接受 Shape,而且只提取 Shape。既然如此,我們能不能創(chuàng)建一個(gè) “ 智能 ” 集合,令其知道自己容納的類型呢?這樣做可消除下溯造型的必要以及潛在的錯(cuò)誤。但在從一個(gè)集合提取對(duì)象句柄時(shí),必須用某種方式準(zhǔn)確地記住它們是什么,以保證下溯造型的正確進(jìn)行。 但這也不是絕對(duì)危險(xiǎn)的,因?yàn)榧偃缦滤菰煨统慑e(cuò)誤的東西,會(huì)得到我們稱為“ 違例 ” ( Except