eb29665ca09f5a692ca4d905f58992e1.ppt
- Количество слайдов: 36
Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 10
Today Ø Multiple inheritance 2
Inheritance is never the only way Given the classes Ø TRAIN_CAR, RESTAURANT how would you implement a DINER? Ø You could have an attribute in TRAIN_CAR train_service: SERVICE Ø Then have RESTAURANT inherit from SERVICE Ø This is flexible if the kind of service may change to a type that is unrelated to TRAIN_CAR Ø Changes in TRAIN_CAR do not affect SERVICE easily 3
Examples of multiple inheritance Combining separate abstractions: Hand s-On Restaurant, train car Ø Calculator, watch Ø Ø Other examples? Teacher, student Ø Home, vehicle Ø 4
Multiple inheritance: Combining abstractions <, <=, >, >=, … NUMERIC COMPARABLE +, –, *, / … (commutative ring) (total order relation) INTEGER REAL STRING COMPLEX 5
Composite figures 6
Multiple inheritance: Composite figures Simple figures A composite figure 7
Defining the notion of composite figure center display hide rotate move … V_LIST [FIGURE ] FIGURE count put remove … COMPOSITE_ FIGURE 8
In the overall structure V_LIST [FIGURE ] FIGURE OPEN_ FIGURE SEGMENT POLYLINE CLOSED_ FIGURE POLYGON perimeter* ELLIPSE perimeter+ diagonal TRIANGLE perimeter+ COMPOSITE_ FIGURE RECTANGLE CIRCLE perimeter++ SQUARE perimeter++ 9
A composite figure as a list before after item Cursor forth 10
Composite figures class COMPOSITE_FIGURE inherit FIGURE V_LIST [FIGURE] feature display -- Display each constituent figure in turn. do from start until after loop . item display end forth Requires dynamic binding end. . . Similarly for move, rotate etc. . 11
An alternative solution: the composite pattern LIST [FIGURE ] FIGURE OPEN_ FIGURE SEGMENT POLYLINE CLOSED_ FIGURE POLYGON figure_list perimeter* ELLIPSE perimeter+ diagonal TRIANGLE perimeter+ COMPOSITE_ FIGURE RECTANGLE CIRCLE perimeter++ SQUARE perimeter++ 12
Lessons from this example Typical example of program with holes We need the full spectrum from fully abstract (fully deferred) to fully implemented classes Multiple inheritance is there to help us combine abstractions 14
Hand s-On Multiple inheritance: Name clashes f B A C f ? 15
Hand s-On Resolving name clashes f B A f rename f as A_f C A_f, f 16
Hand s-On Consequences of renaming Valid or invalid? f a 1 : A b 1 : B c 1 : C. . . c 1. f B A f rename f as A_f Valid a 1. A_f Valid b 1. f A_f, f Invalid c 1. A_f C Valid b 1. A_f Invalid 17
Are all name clashes bad? A name clash must be removed unless it is: Ø Under repeated inheritance (i. e. not a real clash) indirect repeated inheritance Ø direct repeated inheritance Between features of which at most one is effective (i. e. others are deferred) 18
Feature merging f* A f* B C f+ D * Deferred + Effective 19
Feature merging: with different names class D inherit A rename g as f end B C rename h as f end feature. . . end A g* g B f D C f* h h+ f * Deferred + Effective Renaming 20
Feature merging: effective features f+ A f+ f -- B C f+ f -D * Deferred + Effective -- Undefine 21
Undefinition deferred class T inherit S undefine v end feature. . . end 22
Merging through undefinition f+ A class D inherit A undefine f end B C undefine f end feature. . . end f+ B C f+ f -- f -D * Deferred + Effective -- Undefine 23
Merging effective features with different names f+ A g+ class D inherit A undefine f end B C rename g as f undefine f end rename h as f end feature. . . end h+ B g f -- C f h f D 24
Acceptable name clashes If inherited features have all the same names, there is no harmful name clash if: Ø They all have compatible signatures Ø At most one of them is effective Semantics of such a case: Ø Merge all features into one Ø If there is an effective feature, it imposes its implementation 25
Exercise: All-in-one-device PRINTER SCANNER Hand s-On FAX ALL_IN_ONE_DEVICE 27
Hand s-On Exercise: All-in-one-device class PRINTER feature print_page -- Print a page. do print ("Printer prints a page. . . ") end switch_on -- Switch from ‘off‘ to ‘on‘ do print ("Printer switched on. . . ") end class SCANNER feature scan_page -- Scan a page. do print (“Scanner scans a page. . . ") end switch_on -- Switch from ‘off‘ to ‘on‘ do print (“Scanner switched on. . . ") end class FAX feature send -- Send a page over the phone net. do print (“Fax sends a page. . . ") end start -- Switch from ‘off‘ to ‘on‘ do print (“Fax switched on. . . ") end send -- Send data to PC. do print (“Scanner sends data. . . ") end end 28
Exercise: All-in-one-device PRINTER SCANNER Hand s-On FAX ALL_IN_ONE_DEVICE class ALL_IN_ONE_DEVICE inherit. . . end How to resolve the name clashes? Ø switch_on Ø send 29
Exercise: All-in-one-device class ALL_IN_ONE_DEVICE Hand s-On inherit PRINTER rename switch_on as start undefine start end SCANNER rename switch_on as start, send as send_data end FAX rename send as send_message undefine start end feature . . . end 30
Hand s-On Valid or invalid? class ALL_IN_ONE_DEVICE inherit PRINTER rename switch_on as start undefine start end SCANNER rename switch_on as start, send as send_data end FAX rename send as send_message undefine start end feature . . . end s: SCANNER f: FAX a: ALL_IN_ONE_DEVICE Ø a. switch_on Ø a. print_page Ø f. send_message Ø s. switch_on Ø f. send Ø a. send Invalid Valid Invalid 31
A special case of multiple inheritance UNIVERSITY _MEMBER TEACHER id STUDENT ? ? ASSISTANT ? ? This is a case of repeated inheritance 32
Indirect and direct repeated inheritance A A C B D D 33
Multiple is also repeated inheritance A typical case: copy ++ is_equal ++ ANY copy is_equal C LIST D copy C_copy is_equal C_is_equal ? ? 34
Sharing and replication A g g_b f g C B g g_c D Features such as f, not renamed along any of the inheritance paths, will be shared. Features such as g, inherited under different names, will be replicated. 35
The need for select A potential ambiguity arises because of polymorphism and dynamic binding: copy ANY is_equal a 1 : ANY d 1 : D … a 1 : = d 1 a 1. copy (…) C LIST copy ++ is_equal ++ copy C_copy is_equal C_is_equal D 36
Removing the ambiguity class D inherit V_LIST [T ] select end C copy, is_equal rename copy as C_copy, is_equal as C_is_equal, . . . end 37
When is a name clash acceptable? (Between n features of a class, all with the same name, immediate or inherited. ) Ø They must all have compatible signatures. Ø If more than one is effective, they must all come from a common ancestor feature under repeated inheritance. 38


