下文是Delphi7Help中关于Inherited的解释
向各位请教 红色部分是什么意思,E文差的很,麻烦给翻译下,谢谢!
The reserved word inherited plays a special role in implementing polymorphic behavior. It can occur in method definitions, with or without an identifier after it.If inherited is followed by the name of a member, it represents a normal method call or reference to a property or field--except that the search for the referenced member begins with the immediate ancestor of the enclosing method's class. For example, wheninherited Create(...);occurs in the definition of a method, it calls the inherited Create.When inherited has no identifier after it, it refers to the inherited method with the same name as the enclosing method or, if the enclosing method is a message handler, to the inherited message handler for the same message. In this case, inherited takes no explicit parameters, but passes to the inherited method the same parameters with which the enclosing method was called. For example,inherited;occurs frequently in the implementation of constructors. It calls the inherited constructor with the same parameters that were passed to the descendant.

解决方案 »

  1.   

    写过控件的人都知道,这段E文我也不大懂,但我可以告诉你的是:
    如果要想继承父类的方法的代码,就要加,但加的位置也有讲究,有的是在Inherited之前插一段自己的代码,在之后也插一段代码;有的就不用父类的代码,比如从TCustomControl继承下来的控件,在它的Paint方法里就不需要Inherited;根本就不需要父类的绘制代码,而是自己画,加了反而影响绘制速度,因为先是执行父类的绘制代码,最后才执行你的绘制代码.还有很多的消息,加不加的区别很大的,直接影响到消息的派发.这样吧,你可以从TPanel继承一个控件,在它的Paint方法里试一下加不加Inherited的区别.
      

  2.   

    inherited后面followed一个成员名时,表示一个函数调用或一个属性的引用,比较特殊的是:如果inherited后followed一个其直接父类封装的方法,则它会先搜索父类相关方法执行
      

  3.   

    谢谢楼上,
    仔细想想,现在大体明白,
    Inherited后面如果直接跟当前类的方法名时,表示对这个方法的正常调用(貌似不用Inherited关键字也可以啊),如果Inherited后面跟的方法名不属于当前正在定义的类(不直接属于),则从当前类的直接父类开始搜索该方法(搜索父类方法),然后执行.
      

  4.   

    补充:如果Inherited后门什么也不跟,则表示调用父类中跟当前方法同名的方法(参数也相同).现在问题又来了:如果父类中该同名方法有好几个(因为重载),那么Inherited后面未加指示(identifier),那调用的是众多重载方法中的哪个呢?与当前方法参数相同的那个么?如果当前方法也是一个重载方法呢(即参数与父类中的同名方法参数都不同)?个人认为还是,在Inherited后面乖乖的加上具体的方法名和参数吧,偷这点懒,导致一大堆问题,不值得.