当前位置:文档之家› 大学毕业论文---软件专业外文文献中英文翻译

大学毕业论文---软件专业外文文献中英文翻译

软件专业毕业论文外文文献中英文翻译Object landscapes and lifetimesTech nically, OOP is just about abstract data typing, in herita nee, and polymorphism, but other issues can be at least as importa nt. The rema in der of this sect ion will cover these issues.One of the most importa nt factors is the way objects are created and destroyed. Where is the data for an object and how is the lifetime of the object con trolled? There are differe nt philosophies at work here. C++ takes the approach that con trol of efficie ncy is the most importa nt 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 allocatio n and release, and con trol of these can be very valuable in some situati ons. However, you sacrifice flexibility because you must know the exact qua ntity, 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 desig n, warehouse man ageme nt, or air-traffic con trol, this is too restrictive.The sec ond approach is to create objects dyn amically in a pool of memory called the heap. In this approach, you don't know un til run-time how many objects you n eed, what their lifetime is, or what their exact type is. Those are determined at the spur of the moment while the program is runnin g. If you n eed a new object, you simply make it on the heap at the point that you n eed it. Because the storage is man aged dyn amically, at run-time, the amount of time required to allocate storage on the heap is sig ni fica ntly Ion ger tha n the time to create storage on the stack. (Creat ing storage on the stack is ofte n a si ngle assembly in structio n to move the stack poin ter dow n, and ano ther to move it back up.) The dyn amic approach makes the gen erally logical assumpti on that objects tend to be complicated, so the extra overhead of finding storage and releas ing that storage will not have an importa nt impact on the creati on of an object .In additi on, the greater flexibility is esse ntial to solve the gen eral program ming problem.Java uses the sec ond approach, exclusive". Every time you want to create an object, you use the new keyword to build a dyn amic in sta nee of that object.There's ano ther issue, however, and that's the lifetime of an object. With Ian guages 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 kno wledge of its lifetime. In a Ianguage like C++, you must determine programmatically when to destroy theobject, which can lead to memory leaks if you don ' t do it correctly (and this is a com mon problemin C++ programs). Java provides a feature called a garbage collector that automatically discovers whe n an object is no Ion ger in use and destroys it. A garbage collector is much more convenient because it reduces the nu mber of issues that you must track and the code you must write. More importa nt, the garbage collector provides a much higher level of in sura nee aga inst the in sidious problem of memory leaks (which has brought many a C++ project to its kn ees).The rest of this secti on looks at additi onal factors concerning object lifetimes and Ian dscapes.1. The si ngly rooted hierarchyOne of the issues in OOP that has become especially prominent since the in troduct ion of C++ is whether all classes should ultimately be inherited from a single base class. In Java (as with virtually all other OOP Ianguages) th e answer is “ yes ” and the name of this ultimate base class is simply Object. It turns out that the ben efits of the sin gly rooted hierarchy are many.All objects in a sin gly rooted hierarchy have an in terface in com mon, so they are all ultimately the same type. The alter native (provided by C++) is that you don' know that everyth ing is the same fun dame ntal type. From a backward-compatibility sta ndpo int 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 that ' s built into other OOP Ianguages. And in any new ctasBy you acquire, some other in compatible in terface will be used. It requires effort (and possibly multiple in herita nee) to work the new in terface into your desig n. Is the extra “ flexibility ” of C++ worth it? If you n eecit—if you have a large investment in C —it ' s quite valuable. If you ' re starting from scratch, otheralter natives such as Java can ofte n be more productive.All objects in a sin gly rooted hierarchy (such as Java provides) can be guara nteed to have certa in function ality. You know you can perform certa in basic operati ons on every object in your system. A sin gly rooted hierarchy, along with creat ing all objects on the heap, greatly simplifiesargume nt pass ing (one of the more complex topics in C++).A sin gly rooted hierarchy makes it much easier to impleme nt a garbage collector (which is convenien tly built into Java). The n ecessary support can be in stalled 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 impleme nt a garbage collector.Since run-time type information is guaranteed to be in all objects, you ' ll never end up withobject whose type you cannot determ ine. This is especially importa nt with system level operati ons, such as exceptio n han dli ng, and to allow greater flexibility in program ming.2 .Collectio n libraries and support for easy collecti on useBecause a container is a tool that you ' ll 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 you ' ll use frequently, it makes sense to have a library of containers that arebuilt in a reusable fashi on, so you can take one off the shelf and plug it i nto your program. Java provides such a library, which should satisfy most n eeds.Down cast ing vs. templates/ge nericsTo make these containers reusable, they hold the one universal type in Java that was previously men ti on ed: Object. The si ngly rooted hierarchy means that everyth ing 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, whe n you add your object reference into the containerit is upcast to Object, thus los ing its ide ntity. When you fetch it back, you get an Object referen ce, and not a reference to the type that you put in. So how do you turn it back into someth ing that has the useful in terface of the object that you put into the container?Here, the cast is used again, but this time you ' re not casting up the inheritanee hierarchy to a more gen eral type, you cast dow n the hierarchy to a more specific type. This manner of cast ing is called downcasting. With upcasting, you know, for example, that a Circle is a type of Shape so it ' s safe to upcast, but you don ' t knoObjeat annecessarily a Circle or a Shape so it ' s hardlysafe to dow ncast uni ess you know that s what you ' re dealing with.It ' s not completely dangerous, however, because if you downcast to the wrong thing you get a run-time error called an excepti on, 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 dow ncast.Down cast ing and the run-time checks require extra time for the running program, and extraeffort from the programmer. Would n make sense to somehow create the container so that itknows the types that it holds, elim in at ing the n eed for the dow ncast and a possible mistake? The soluti on 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 importa nt part of C++, partly because C++ has no sin gly rooted hierarchy .In C++, theJava cu keyword that impleme nts parameterized types is “ template.has no parameterized types since it is possible for it to get by —however awkwardly —using the sin gly rooted hierarchy. However, a curre nt proposal for parameterized types uses a syn tax that is strik in gly similar to C++ templates.对象的创建和存在时间从技术角度说,00P(面向对象程序设计)只是涉及抽象的数据类型、继承以及多形性,但另一些问题也可能显得非常重要。

相关主题