父类在另一个类中

发布于 2024-09-25 06:42:07 字数 431 浏览 0 评论 0原文

好的,我有这个类,比如说 CMain,它包含一个 CFruit 类。我想做的是根据 CFruit 的类型运行函数(如果是 CPear 或 CApple 等)。所以我想做这样的事情:

type CMain = class
   myFruit : CFruit;
   function GetFruit() : CFruit;
   procedure SetFruit( Fruit : CFruit ); 
end;

procedure CMain.SetFruit( Fruit : CFruit );
begin
  if Fruit.IsPear then .. else etc;
end;

...显然编译器阻止我这样做,因为 CFruit 只是 CPear 和 CApple 的父级。我有什么可行的方法可以做到这一点吗? (不可能制作单独的 CMain)。谢谢。

Okay, so I have this class, let's say CMain, that contains a CFruit class. What I would like to do is run functions based on CFruit's type (if it's CPear or CApple, etc). So I'd like to do something like this:

type CMain = class
   myFruit : CFruit;
   function GetFruit() : CFruit;
   procedure SetFruit( Fruit : CFruit ); 
end;

procedure CMain.SetFruit( Fruit : CFruit );
begin
  if Fruit.IsPear then .. else etc;
end;

...obviously the compiler stops me from doing this because CFruit is just CPear and CApple's parent. Is there any viable way I can do this? (making sepparate CMain's is out of the question). Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冧九 2024-10-02 06:42:07

IIUC 你想要虚拟方法

IIUC you want virtual methods.

魂归处 2024-10-02 06:42:07

实际上有一个“is”运算符,它将检查对象是否是类的实例或其祖先。这称为“动态类型检查”并且有点高级。检查帮助以获得说明。

根据您的需求,“虚拟方法”可能就是您所需要的,正如其他人所解释的那样。请检查发布的有关“虚拟方法”的链接作为正确的 OOP 方式。

在下面的例子中

if AFruit is TApple then

if AFruit is TFruit then

都返回 true

type
  TFruit = class
  protected
    FName: string;
  public
    property Name: string read FName;
  end;

  TApple = class(TFruit)
  public
    constructor Create;
  end;

  TPear = class(TFruit)
  public
    constructor Create;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    mixed: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FMixValue: string;
    procedure MixFruits(AFruit: TFruit);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  APear: TPear;
  AApple : TApple;
begin
  APear := TPear.Create;
  AApple := TApple.Create;
  MixFruits(APear);
  MixFruits(AApple);
  mixed.Caption := FMixValue;
end;

{ TPear }

constructor TPear.Create;
begin
  inherited;
  FName := 'Pear';
end;

{ TApple }

constructor TApple.Create;
begin
  inherited;
  FName := 'Apple';
end;

procedure TForm1.MixFruits(AFruit: TFruit);
begin
  FMixValue := FMixValue + ' ' + AFruit.Name;
  if AFruit is TApple then
    ShowMessage('An Apple')
  else if AFruit is TPear then
    ShowMessage('A Pear')
  else
    ShowMessage('What is it?');
end;

Actually there is an "is" operator, that will check if the Object is an instance of class or it's ancestors. This is called "dynamic type checking" and is sorta advanced. Check the help for a clarification.

Depending on your needs "virtual methods" could be what you need as explained by others. Please check the link posted about "virtual methods" as the correct OOP way.

In the example below

if AFruit is TApple then

and

if AFruit is TFruit then

both return true

type
  TFruit = class
  protected
    FName: string;
  public
    property Name: string read FName;
  end;

  TApple = class(TFruit)
  public
    constructor Create;
  end;

  TPear = class(TFruit)
  public
    constructor Create;
  end;

  TForm1 = class(TForm)
    Button1: TButton;
    mixed: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    FMixValue: string;
    procedure MixFruits(AFruit: TFruit);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  APear: TPear;
  AApple : TApple;
begin
  APear := TPear.Create;
  AApple := TApple.Create;
  MixFruits(APear);
  MixFruits(AApple);
  mixed.Caption := FMixValue;
end;

{ TPear }

constructor TPear.Create;
begin
  inherited;
  FName := 'Pear';
end;

{ TApple }

constructor TApple.Create;
begin
  inherited;
  FName := 'Apple';
end;

procedure TForm1.MixFruits(AFruit: TFruit);
begin
  FMixValue := FMixValue + ' ' + AFruit.Name;
  if AFruit is TApple then
    ShowMessage('An Apple')
  else if AFruit is TPear then
    ShowMessage('A Pear')
  else
    ShowMessage('What is it?');
end;
已下线请稍等 2024-10-02 06:42:07

下面是一个使用虚方法的例子:

type 
TFruit = class
public
  procedure doSomethingFruitSpecific; virtual; abstract;
end;

TPear = class(TFruit)
public
  procedure doSomethingFruitSpecific; override;
end;

TApple = class(TFruit)
public
  procedure doSomethingFruitSpecific; override;
end;

TMain = class
   procedure SetFruit( Fruit : TFruit ); 
end;

implementation

procedure TMain.SetFruit( Fruit : TFruit );
begin
  Fruit.doSomethingFruitSpecific;
end;

procedure TApple.doSomethingFruitSpecific;
begin
  Writeln('bake an apple pie');
end;

procedure TPear.doSomethingFruitSpecific;
begin
  Writeln('pick some pears');
end;

Here is an example for the use of virtual methods:

type 
TFruit = class
public
  procedure doSomethingFruitSpecific; virtual; abstract;
end;

TPear = class(TFruit)
public
  procedure doSomethingFruitSpecific; override;
end;

TApple = class(TFruit)
public
  procedure doSomethingFruitSpecific; override;
end;

TMain = class
   procedure SetFruit( Fruit : TFruit ); 
end;

implementation

procedure TMain.SetFruit( Fruit : TFruit );
begin
  Fruit.doSomethingFruitSpecific;
end;

procedure TApple.doSomethingFruitSpecific;
begin
  Writeln('bake an apple pie');
end;

procedure TPear.doSomethingFruitSpecific;
begin
  Writeln('pick some pears');
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文