同上

解决方案 »

  1.   

    帮助中是这样说的, 帮助是最好的老师,  你在这里问并不一定能得到正确恰当的答案(俺是来蹭分的:))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 a method identifier, it represents a normal method call, except that the search for the method begins with the immediate ancestor of the enclosing method抯 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. In this case, inherited can appear with or without parameters; if no parameters are specified, it 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.
      

  2.   

    继承父类中相同名称的函数或者过程。
    如果是消息的话,有点例外。参见
    http://expert.csdn.net/Expert/topic/2121/2121889.xml?temp=.5554926
      

  3.   

    我以前写的学习总结:
    虚拟方法和动态方法不同于静态方法,它们可以在其后裔类中被覆盖(overridden),即只有一个方法所在类的祖先类对应的方法是virtual或dynamic的,这个当前类的方法才可以用overide覆盖。当一个覆盖方法被调用时,方法调用中使用的类或对象的实际(运行时)类型决定了哪一个实现是有效的,而非变量声明的类型决定。virtual方法是可以有实现的部分的,也可是在后裔类中override。abstract抽象方法是那些在类中声明但未实现的虚拟方法或动态方法。抽象方法的实现推延到后裔类中。声明抽象方法必需在指示字virtual或dynamic之后使用abstract。例如:procedure DoSomething; virtual; abstract;  即abstract方法是没有实现的,实现推延到后裔类中,后裔override这个方法后可以有实现部分。
    如果:
    TBaseObj=class
      public
        procedure AMethod;
      end;TChildObj=class(TBaseObj)
        procedure AMethod;
      end;{ TBaseObj }procedure TBaseObj.AMethod(str:string);
    begin
      showmessage('TBaseObj.AMethod;');
    end;{ TObj }procedure TChildObj.AMethod(str:string);
    begin
      showmessage('TChildObj.AMethod;');
    end;实际上两个类的AMethod方法无任何关系。子类中将看不到祖先类的AMethod方法,而只有自已的AMethod方法,运行下面事件过程将执行
     showmessage('TChildObj.AMethod;');procedure TForm1.Button2Click(Sender: TObject);
    var
      childobj:TChildObj;
    begin
      childobj:=TChildObj.Create;
      childobj.AMethod;end;如果TBaseObj=class
      public
        procedure AMethod(str:string;i:integer=0);virtual;
      end;TChildObj=class(TBaseObj)
        procedure AMethod(str:string);
      end;
    pprocedure TBaseObj.AMethod(str:string;i:integer=0);
    begin
      showmessage('Base'+str);
    end;{ TObj }procedure TChildObj.AMethod(str:string;i:integer=0);
    begin
      showmessage('Child'+str);
    end;执行下面事件过程,将看到showmessage中显示'ChildAMethod;'  并有[Warning] Unit1.pas(28): Method 'AMethod' hides virtual method of base type 'TBaseObj'的编译信息显示,说明在子类没有用override关键字时,声明一个与祖先类同名的方法是会隐藏祖先类的同名方法,执行自已的方法。而在子类方法声明后加reintroduce就不会有编译信息显示。如果使用override,要保证子类与祖先类方法的参数类型,顺序返回值相同,不然会在编译时报错:
    因此交上面子类声明改为:procedure AMethod(str:string;i:integer=0);override;,实现部分也要相应更改,这样执行下面事件过程,即子类方法覆盖了祖先类方法最后将看到showmessage中显示'ChildAMethod;' ,如果有多个子类,都override那个virtual的祖先类的方法,那么最后执行结果是子类的这个AMethod方法,覆盖了祖先类同名方法的实现内容。实际上如果祖先类的这个方法是virtual;abstract的结果也是一样的,。即祖先类只定义一个声明,具体实现由这个祖先类的子类们去完成,执行不同子类结果不同,因为各子类的AMethod实现细节都有所不同。procedure TForm1.Button2Click(Sender: TObject);
    var
      childobj:TChildObj;
    begin
      childobj:=TChildObj.Create;
      childobj.AMethod('AMethod;');end;另一个有用的东西是:inherited关键字,如果将上面的子类方法实现改为:
    procedure TChildObj.AMethod(str:string;i:integer=0);
    begin
      inherited;
      showmessage('Child'+str);
    end;
    然后再运行Button2Click,则会先看到showmessage出BaseAMethod,再看到showmessage出ChildAMethod。为什么会有这种现象呢,看一下关于inherited的说明:
      保留字inherited在实现多种行为中扮演特殊的角色。它可以出现在方法定义中,在其后面可以有或没有标识符。
    如果inherited之后跟随一个成名名称,那么除了表示在封装了方法的类的直接祖先中搜寻成员之外,还可以表示标准的方法调用或者对属性或域的引用。也就是说运行到inherited时会告诉当前inherited所在方法中祖先类的同名方法,所以先看到showmessage出BaseAMethod。如果祖先类中有一个abc方法,那么在此也可调用,即 inherited abc,即如果inherited后面没参数,执行时找祖先类同名方法,有参数如abc则在祖先类找那个
    abc方法。