elements of the address, an address object has an operation for printing the address.
An address may be used for describing persons and companies. One approach is to make
This is a typical example of how inheritance should not be used. A Person or Company is
address as a component (part-object) of a Person or Company. Using instances of class
needs different bindings corresponding to Company and Customers. In most languages
it is necessary to introduce additional classes for this purpose [MM92]. Using part-objects
and introducing additional classes, the above example may be rewritten as follows:
CompanyAddress it is not possible to refer to the name and director attributes of
Company as an argument. The example does not show this argument.
In C++ it is possible to declare PersonAddress and CompanyAddress within
Person and Company, but it is not possible to refer to variables in the enclosing Person
and Company objects thus C++ only offers half of the functionality of block structure as
mentioned above.
In a block-structured language, PersonAddress and CompanyAddress may be
declared within Person and Company and thereby solving the scope problem. Thus in
BETA it is possible to declare the PersonAddress and CompanyAddress classes
within Person and Company. Moreover since BETA also supports singular objects, it is
not even necessary to introduce the additional classes:
Person:
(#
name:
...;
adr: @address(# printLabel::<(#do {print name} #)#)
#);
Company:
(# name,director: ...;
adr: @ address
(#
printLabel::<
(#do {print name and director} #)#)
#)
As the example shows, BETA supports inheritance from part objects in a similar way to
inheritance from super patterns. Inheritance from part-objects is often used in this situation,
since this makes it possible to bind/redefine the virtuals of, in this case, Address.
Furthermore it is possible to inherit from multiple part objects. In the following
example, the class pattern T inherits from two part objects, being instances of class A and B
respectively:
T: (# x: @ A (# f1::< (# ... #);
f2::<
(#
...
#);
#);
y: @ B (# g1::< (# ... #) #);
...
#)
The x-objects binds the virtual patterns f1 and f2 and the y-objects binds the virtual
pattern g1. In all 3 virtual bindings, it is possible to refer to variables in the enclosing T-
class. This form of multiple inheritance from part objects is technically as powerful as
multiple inheritance without overlapping superclasses suggested in Reference [Kro85] and
corresponds to multiple inheritance in C++ with non-virtual base classes [Str87].
Do'stlaringiz bilan baham: