正在加载图片...
1174 GENERICITY VERSUS INHERITANCE $B.2 Objects of type TAPE will possess all the features of type DEVICE,possibly adapted (in the case of open),and complemented by the new feature rewind. With inheritance comes polymorphism,permitting assignments of the formx=y,This is approximate but only if the type ofx is an ancestor of the type ofy.The next associated property is terminology:“isan dynamic binding:if x is a device,the call x.open (f7)will be executed differently ancestor of"stands for“conforms to” depending on the assignments performed onx before the call:afterx=y,wherey is a tape, Precise rules appear the call will execute the tape version. in earlier chapters. We have seen the remarkable benefits of these inheritance techniques for reusability and extendibility.A key aspect was the Open-Closed principle:a software element such as DEIICE is both usable as it stands (it may be compiled as part of an executable system) and still amenable to extensions (if used as an ancestor of new classes). Next come deferred features and classes.Here we note that Unix devices are a special kind of file;so you may make DEVICE an heir to class FILE,whose other heirs might include TEXT FILE (itself with heirs NORMAL and DIRECTORY)and BINARY FILE. The figure shows the inheritance graph,a tree in this case. open A simple FILE close inheritance hierarchy,with deferred and effective classes DEVICE TEXT FILE BINARY FILE Inherits from DEVICE BINARY FILE *Deferred Although it is possible to open or close any file,how these operations are performed depends on whether the file is a device,a directory etc.So FILE is a deferred class with deferred routines open or close,making descendants responsible for implementing them: deferred class FILE feature open (file_descriptor:INTEGER)is deferred end close is deferred end. end--class FILE Effective descendants of F/LE will provide effective implementations of open and close.1174 GENERICITY VERSUS INHERITANCE §B.2 Objects of type TAPE will possess all the features of type DEVICE, possibly adapted (in the case of open), and complemented by the new feature rewind. With inheritance comes polymorphism, permitting assignments of the form x := y, but only if the type of x is an ancestor of the type of y. The next associated property is dynamic binding: if x is a device, the call x ● open (f1) will be executed differently depending on the assignments performed on x before the call: after x := y, where y is a tape, the call will execute the tape version. We have seen the remarkable benefits of these inheritance techniques for reusability and extendibility. A key aspect was the Open-Closed principle: a software element such as DEVICE is both usable as it stands (it may be compiled as part of an executable system) and still amenable to extensions (if used as an ancestor of new classes). Next come deferred features and classes. Here we note that Unix devices are a special kind of file; so you may make DEVICE an heir to class FILE, whose other heirs might include TEXT_FILE (itself with heirs NORMAL and DIRECTORY) and BINARY_FILE. The figure shows the inheritance graph, a tree in this case. Although it is possible to open or close any file, how these operations are performed depends on whether the file is a device, a directory etc. So FILE is a deferred class with deferred routines open or close, making descendants responsible for implementing them: deferred class FILE feature open (file_descriptor: INTEGER) is deferred end close is deferred end; end -- class FILE Effective descendants of FILE will provide effective implementations of open and close. This is approximate terminology; “is an ancestor of ” stands for “conforms to”. Precise rules appear in earlier chapters. A simple inheritance hierarchy, with deferred and effective classes ∗ FILE DEVICE Inherits from TEXT_FILE BINARY_ FILE ∗ Deferred DEVICE BINARY_ FILE open* close*
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有