gpt4 book ai didi

delphi - Objective Pascal - 你如何确保在作为祖先时调用被告的方法?

转载 作者:行者123 更新时间:2023-12-03 18:27:16 25 4
gpt4 key购买 nike

我目前正在创建一个小型 RPG 引擎。我刚刚到了为游戏对象实现类树的地步,但我不能 100% 确定我应该在什么地方以及在什么情况下使用哪些方法指令。

我查看了 Free Pascal Complier 的引用指南和其他各种互联网资源,但我仍然没有信心。

我计划实现游戏对象处理的方式要求游戏对象的类别具有公共(public)基类,并将存储在该类的数组中,从而将它们转换为祖先。

我主要需要了解的是如何确保当我将一个类转换为它的祖先之一时,它调用实际类的重载/覆盖/重新引入方法而不是祖先的方法。

谢谢你的时间!

如果我不够清楚,我很乐意在这里澄清/重述任何内容。

最佳答案

当您将它们放入其父类的数组时,您不是在“强制转换”,而且您不应该这样做;这不是继承和多态的工作方式。

您在父级(祖先)中创建一个虚拟方法,然后每个子级继承父级的方法并用自己的特定行为覆盖它。当您调用父方法时,多态性确保调用正确的方法。

这是一个快速(简单)的示例,说明应该如何构建这样的东西。它采用控制台应用程序的形式,因此您可以实际运行它来查看输出。

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
System.SysUtils;

type
// Base class
TAnimal=class
procedure Sit; virtual; // Virtual keyword *necessary* for things to work
procedure Speak; virtual;
end;

// Descendent classes
TDog=class(TAnimal)
procedure Sit; override; // Override keyword required here
procedure Speak; override;
end;

TCat=class(TAnimal)
// No implementation of Sit, because cats don't sit on command
procedure Speak; override; // Override required here also
end;

// Array type to hold them, so we don't have to typecast anything
TAnimalArray = array of TAnimal;

// Implementation of classes

{ TAnimal }
procedure TAnimal.Sit;
begin

end;

procedure TAnimal.Speak;
begin
// Parent does nothing
end;

{ TCat }
procedure TCat.Speak;
begin
inherited;
WriteLn('Meow.');
end;

{ TDog }
procedure TDog.Sit;
begin
inherited;
WriteLn('Sitting down now.');
end;

procedure TDog.Speak;
begin
inherited;
Writeln('Woof! Woof!');
end;

// Test code to demonstrate use of inheritance and polymorphism
var
Animals: TAnimalArray;
i: Integer;
Pet: TAnimal; // Variable that holds parent type (TAnimal)

const
NumberOfAnimals = 5;

begin
SetLength(Animals, NumberOfAnimals);

// Fill array with a mix of both dogs and cats
for i := 0 to High(Animals) do
begin
if Odd(i) then
Animals[i] := TDog.Create
else
Animals[i] := TCat.Create;
end;

// Loop to use each one regardless of which type, by just accessing the
// virtual Speak method they overrode from their parent class

for Pet in Animals do
begin
Pet.Speak;
Pet.Sit; // Call method only defined for TDog
end;

WriteLn(''); // Blank line in console to separate loops.

// If FreePascal doesn't support the enumeration (for..in) method, do it
// using a counter:

for i := 0 to High(Animals) do
begin
Animals[i].Speak;
Animals[i].Sit; // Call method only defined for TDog
end;

// In real life code, you'd loop through the array and free each one here.
// In this test code, we're exiting right away, and there's really no point
Readln;
end.

以上产生输出:
Meow.
Woof! Woof!
Sitting down.
Meow.
Woof! Woof!
Sitting down.
Meow.

Meow.
Woof! Woof!
Sitting down.
Meow.
Woof! Woof!
Sitting down.
Meow.

您可以在 docwiki 找到关于 Delphi 的 OOP 实现的相当完整的讨论。 .虽然它不是 FreePascal,但它大部分是兼容的,因此它应该有点等价。我不知道有任何类似 FPC 的文档。

关于delphi - Objective Pascal - 你如何确保在作为祖先时调用被告的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com