简体   繁体   English

埃菲尔的多重继承

[英]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. Hoi,eiffel中的多重继承让我很困惑,任何人都可以告诉我French_Us_Driver从其所有父级继承的是什么类。

 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

   inherit

     French_Driver

     Us_Driver
   end

 end --French_Us_Driver

Now has French_Us_Driver features : Us_violation , French_violation and violation 现在有French_Us_Driver功能: Us_violationFrench_violationviolation

or : Us_violation , French_violation 或者: Us_violationFrench_violation

Thanks 谢谢

The class French_Driver only renames a feature violation , it does not add any new one. French_Driver类只重命名一个功能violation ,它不会添加任何新功能。 So, we can call only French_violation on it and not violation (there is no such name anymore in this class because of renaming). 因此,我们只能调用French_violation而不是violation (由于重命名,此类中不再存在此类名称)。 The same is true for the class Us_Driver . 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 . 其结果是类French_Us_Driver继承的功能French_violationFrench_Driver和功能Us_violationUs_Driver ,所以有两个特点总数: French_violationUs_violation

The story would stop here if there were no common ancestor where the feature violation originates from. 如果没有共同特征violation来源的共同祖先,故事就会停在这里。 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 . 因为类French_Us_Driver现在有2个版本的功能violation继承自类Driver (一个版本名为French_violation而另一个版本名为Us_violation ),所以当在类Driver调用功能violation时,不清楚应该使用哪个版本对象的类型是French_Us_Driver The conflict should be resolved by adding a select clause to one of the parent clauses, for example: 应该通过向其中一个parent子句添加select子句来解决冲突,例如:

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. 然后,当在类型为French_Us_Driver的对象上从类Driver调用功能violation时,实际上将调用功能French_violation

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. 这是一个考虑使用select可能是你想要的。 (I will up vote the other answer as is correct except gives no help on how to use select.) (我会在正确的情况下投票给另一个答案,除非没有提供有关如何使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM