我自定义了一个继承类如下:
a=class
b=class(a)
c=class(a)
b和c里分别声明一个函数f为virtual.
在程序里调用的时候为什么这个f没有?
程序里这样做的:
mya:a;
...
a:=b.create//constructor
a.   <-没有f

解决方案 »

  1.   

    a当然没有f,因为F没有在a中声明。
    一般的写法是在a声明一个函数f,virtual或abstract,然后再b,c中进行重载
      

  2.   

    不是,最后那行写错了,是mya:=b.create或c.create;
    mya.  <-没有f,mya已经声明成b or c的实例了,应该有他们的成员呀?
      

  3.   

    unit2unit Unit2;interface
    type
      a=class
      private
        {     }
      public
        {     }
        function f():string;virtual;abstract;
    end;  Tb=class(a)
      private
        {       }
      public
        {       }
        function f():string;override;
    end;implementation  function Tb.f():string;
      begin
          result:='ok';
      end;end.unit1unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs,Unit2;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;
      b:Tb;
    implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      b:=tb.Create;
      showmessage(b.f );
    end;end.
      

  4.   

    shinlee(我是一条活泼的鱼) :你在父类里声明一个纯虚,不是我想要的答案。我的意思是
    a=class;b=class(a);c=class(a);//b里有一public函数:function f:string;virtual;
    //程序里...
    mya:a;
    a:=b.create;//constructor
    a.   <-他的成员里找不到f.//我的疑问在于上一句有了,这里怎么找不到f??
      

  5.   

    shinlee(我是一条活泼的鱼) :你在父类里声明一个纯虚,不是我想要的答案。我的意思是
    a=class;b=class(a);c=class(a);//b里有一public函数:function f:string;virtual;
    //程序里...
    mya:a;
    a:=b.create;//constructor
    a.   <-他的成员里找不到f.//我的疑问在于上一句有了,这里怎么找不到f??
      

  6.   

    靠向上引用也不是这样的.A里当然没有了,A你没有声明定义当然没有,因为这里A才是基类.虚涵数都是要定义在基类里的,这样你向上引用才有意义,不让你以为编译器认得呀.(
      

  7.   

    靠向上引用也不是这样的.A里当然没有了,A你没有声明定义当然没有,因为这里A才是基类.虚涵数都是要定义在基类里的,这样你向上引用才有意义,不让你以为编译器认得呀.(
      

  8.   

    楼上回答的是对的。a没有方法f是因为a的vmt中不含有f方法。