multiple inheritance in Eiffel
Hoi,The multiple inheritance in eiffel really confused me ,can anybody tell me what class French_Us_Driver
inherit from all its parent-class.
class Driver
feature(ANY)
violation
end
end --end Driver
class French_Driver
inherite
Driver
rename
violation as French_violatin
end
end -- end French_Driver
class US_Driver
inherit
Driver
rename
violation as Us_violation
end
end --end Us_Driver
class French_Us__Driver
开发者_StackOverflow中文版 inherit
French_Driver
Us_Driver
end
end --French_Us_Driver
Now has French_Us_Driver
features : Us_violation
,French_violation
and violation
or : Us_violation
,French_violation
Thanks
The class French_Driver
only renames a feature violation
, it does not add any new one. So, we can call only French_violation
on it and not violation
(there is no such name anymore in this class because of renaming). The same is true for the class Us_Driver
.
As a result the class French_Us_Driver
inherits a feature French_violation
from French_Driver
and a feature Us_violation
from Us_Driver
, so there are two features in total: French_violation
and Us_violation
.
The story would stop here if there were no common ancestor where the feature violation
originates from. Because the class French_Us_Driver
now has 2 versions of the feature violation
inherited from the class Driver
(one version is named French_violation
and the other one - Us_violation
), it's not clear which one should be used when calling a feature violation
in the class Driver
when the type of the object is French_Us_Driver
. The conflict should be resolved by adding a select
clause to one of the parent clauses, for example:
class French_Us_Driver inherit
French_Driver
select French_violation end
Us_Driver
end
Then, when a feature violation
is called from the class Driver
on an object of type French_Us_Driver
, the feature French_violation
will actually be called.
Finally, it's possible to merge two versions of the feature into one by giving them the same name which does not necessary match the name of the origin (the code below omits the other details of feature redeclaration and assumes for simplicity that all the features are deferred):
class French_Us_Driver inherit
French_Driver
rename French_violation as French_Us_violation end
Us_Driver
rename Us_violation as French_Us_violation end
end
Don't just select at random. Here is a considered use of select that may be what you want. (I will up vote the other answer as is correct except gives no help on how to use select.)
class
French_Us_Driver
inherit
Driver
select
violation
end
French_Driver end
Us_Driver end
feature
violation
inspect country
when france then French_violation
when usa then Us_violation
end --inspect
end --violation
end --class
精彩评论