- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在创建一个小型 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.
关于delphi - Objective Pascal - 你如何确保在作为祖先时调用被告的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21022000/
说我有两个文件,characters.pas和ogre.pas。食人魔是一个角色,但为了干净起见,我试图将两个文件分开。在characters.pas中,我有 unit Characters; {$m
我想要一个编程代码来打印范围之间的奇数来教学生。这里我取的范围是 1 到 10。所以我想打印 1 到 10 之间的奇数。 我写了这个代码来打印 1 到 10 之间的奇数 program printOd
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 4年前关闭。 Improve this
我创建了一个函数,其中包含 2 个具有相同数据类型的参数,对此我没有问题。 但是我在处理不同的数据类型时遇到了问题 这是我的代码: uses crt; function inputscore(name
我只是想知道如何在 pascal 中进行类型检查?我已经搜索了几个小时,但我找不到任何有用的东西。 例子: var number: Integer; begin write('Enter a nu
我正在实现 an assemblinker for the 16-bit DCPU来自游戏 0x10c。 有人向我建议的一种技术是使用“覆盖,就像过去在 Turbo Pascal 中一样”,以便在运行
Q1:这是什么意思:WriteLn (#$0b) ? $0b应该是十六进制,如 0x0b ,但是 # 呢?标志? 问题 2: x:=readkey; if ( x = #5) do... 是否#5意思
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
对于在 Pascal 中返回值的函数,赋值 FunctionName := SomeVal;用来。我认为它不会像 return 那样在那个确切的地方停止函数执行在 C 中。有没有类似C的东西retur
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我正在开展我的学校项目,我想使用动态(而不是静态)数组。我使用 ObjectPascal,所以我习惯了一些语法。但现在我在旧的 TurboPascal 中编程(我使用的是 Windows 版 Turb
对于这项任务,我有一个不太好的解决方案,但有人可以告诉我如何使用标准库或更优雅的方法。当前的解决方案基于这样的事实:对对象的过程的引用在内存中存储两个指针,一个指向过程代码的指针和一个指向对象的指针。
我正在运行一个 Fenuc Karel 机器人进行类作业,它使用 Pascal 的变体,但我们的机器人是在 1991-1993 年添加 random() 之前的。有谁知道如何在 Pascal 的旧 d
我正在尝试在 Pascal 中创建一个类,我对声明和语法有点困惑。主要的事情是一个错误,我收到“前向声明未解决 Tetromino.Rotate(LongInt)”,我读到我需要在实现部分声明我的过程
我认为展示问题的最简单方法是举个例子。编码: PROGRAM CONSTANTSTRING(OUTPUT); CONST C_MaxLength = 30; VAR small_str
我在 Windows 上运行 Lazarus。我真的很想制作“Beep”程序。看来你可以在 Pascal 中使用: windows.beep(300,500); 但不是在拉撒路!我可以使用另一个命令吗
我正在对一个 18 岁的 pascal 程序进行维护。为了帮助我理解一切是如何组合在一起的,我想绘制一个调用图。但是我找不到任何可以为 pascal 源绘制调用图的软件。我目前正在使用 Turbo P
我正在使用帕斯卡。我在处理读取文件时遇到问题。 我有一个带有整数的文件。我读取文件的 pascal 是: read(input, arr[i]); 如果我的文件内容是 1 2 3那么它很好,但如果它是
当分配运算符 := 时会发生什么?在 Object Pascal 中重载?我主要是指首先评估什么,更重要的是如何(如果可能)更改此顺序。这是一个让我烦恼的例子: 我声明TMyClass因此: TMyC
如何使用 writeln 在 Pascal 中打印撇号符号功能? 例子: writeln('My brother's book'); 无法工作,因为 s book没有“写”函数,所以编译器返回一个错误
我是一名优秀的程序员,十分优秀!