正在加载图片...
$7.3 THE ROLE OF CLASSES 169 Pursuing an analogy used earlier,we may compare this situation to that of a Prentice Hall employee who is in charge of preparing the catalog of software engineering titles.For the catalog writer,OOSC,the concept behind this book,is an object-an instance of a class "catalog entry".In contrast,for the reader of the book,that concept is a class,of which the reader's particular copy is an instance. Some object-oriented languages,notably Smalltalk,have introduced a notion of metaclass to handle this kind of situation.A metaclass is a class whose instances are themselves classes-what the Name of the Rose extract called"signs of signs". We will avoid metaclasses in this presentation,however,since they bring more problems than benefits.In particular,the addition of metaclasses makes it difficult to have static type checking,a required condition of the production of reliable software.The main applications of metaclasses are better obtained through other mechanisms anyway: “Universal c☑lasses” You can use metaclasses to make a set of features available to many or all classes page 580. We will achieve the same result by arranging the inheritance structure so that all classes are descendants of a general-purpose,customizable class ANY,containing the declarations of universal features. See“The creation A few operations may be viewed as characterizing a class rather than its instances, instruction",page justifying their inclusion as features of a metaclass.But these operations are few and 232. known;the most obvious one is object creation-sufficiently important to deserve a special language construct,the creation instruction.(Other such operations,such as object duplication,will be covered by features of class ANY.) There remains the use of metaclasses to obtain information about a class,such as a browser may need:name of the class,list of features,list of parents,list of suppliers etc.But we do not need metaclasses for that.It will suffice to devise a library class, E CLASS,so that each instance of E CLASS represents a class and its properties. When we create such an instance,we pass to the creation instruction an argument representing a certain class C;then by applying the various features of E CLASS to that instance,we can learn all about C. In practice,then,we can do without a separate concept of metaclass.But even in a method,language or environment that would support this notion,the presence of metaclasses is no excuse for confusing molds and their instances-classes and objects. 7.3 THE ROLE OF CLASSES Having taken the time to remove an absurd but common and damaging confusion,we may now come back to the central properties ofclasses,and in particular study why they are so important to object technology. To understand the object-oriented approach,it is essential to realize that classes play two roles which pre-O-O approaches had always treated as separate:module and type.§7.3 THE ROLE OF CLASSES 169 Pursuing an analogy used earlier, we may compare this situation to that of a Prentice Hall employee who is in charge of preparing the catalog of software engineering titles. For the catalog writer, OOSC, the concept behind this book, is an object — an instance of a class “catalog entry”. In contrast, for the reader of the book, that concept is a class, of which the reader’s particular copy is an instance. Some object-oriented languages, notably Smalltalk, have introduced a notion of metaclass to handle this kind of situation. A metaclass is a class whose instances are themselves classes — what the Name of the Rose extract called “signs of signs”. We will avoid metaclasses in this presentation, however, since they bring more problems than benefits. In particular, the addition of metaclasses makes it difficult to have static type checking, a required condition of the production of reliable software. The main applications of metaclasses are better obtained through other mechanisms anyway: • You can use metaclasses to make a set of features available to many or all classes. We will achieve the same result by arranging the inheritance structure so that all classes are descendants of a general-purpose, customizable class ANY, containing the declarations of universal features. • A few operations may be viewed as characterizing a class rather than its instances, justifying their inclusion as features of a metaclass. But these operations are few and known; the most obvious one is object creation — sufficiently important to deserve a special language construct, the creation instruction. (Other such operations, such as object duplication, will be covered by features of class ANY.) • There remains the use of metaclasses to obtain information about a class, such as a browser may need: name of the class, list of features, list of parents, list of suppliers etc. But we do not need metaclasses for that. It will suffice to devise a library class, E_CLASS, so that each instance of E_CLASS represents a class and its properties. When we create such an instance, we pass to the creation instruction an argument representing a certain class C; then by applying the various features of E_CLASS to that instance, we can learn all about C. In practice, then, we can do without a separate concept of metaclass. But even in a method, language or environment that would support this notion, the presence of metaclasses is no excuse for confusing molds and their instances — classes and objects. 7.3 THE ROLE OF CLASSES Having taken the time to remove an absurd but common and damaging confusion, we may now come back to the central properties of classes, and in particular study why they are so important to object technology. To understand the object-oriented approach, it is essential to realize that classes play two roles which pre-O-O approaches had always treated as separate: module and type. “Universal classes”, page 580. See “The creation instruction”, page 232
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有