Hoi allemaal,
Ik heb weer eens een zeer basale vraag, dit keer over
object georienteerd programmeren. Ik heb onderstaande code van het
internet gehaald en gecopieerd in mijn Form1 code.
Maar nu geeft hij een foutmelding. Hij zegt namelijk
'[Error] Unit1.pas(69): 'Self' is uninitialized. An inherited constructor must be called'
Weten jullie wat er aan de hand is? Ik heb trouwens Delphi 8. Heeft
het daarmee te maken? Als het echt veel werk is laat maar zitten hoor.
Maar volgens mij kan het niet echt moeilijk zijn. Ik heb trouwens regel 69 aangegeven.
Alvast heel erg bedankt,
Vinny
Code:
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
// Define a Ball class
TBall = class
protected
ballSize : Byte;
ballSpeed : Byte;
published
procedure Kick(power : Byte); virtual;
function GetSpeed : Byte;
constructor Create(size : Byte);
end;
// Define a specialised ball
TFootball = class(TBall)
private
ballPanels : Byte;
published
// Now we override the TBall kick method to take into account
// the number of panels of the ball
procedure Kick(power : Byte); virtual;
// Different constructor - must pass panels now
constructor Create(size : Byte; panels : Byte);
end;
// definition of the form this unit uses
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
// Ball method implementations
procedure TBall.Kick(power : Byte);
begin
ballSpeed := (power * ballSize) DIV 4;
end;
function TBall.GetSpeed : Byte;
begin
Result := ballSpeed;
end;
constructor TBall.Create(size : Byte);
begin
ballSize := size;
ballSpeed := 0;
end; {Dit is regel 69}
// Football method implementations
procedure TFootball.Kick(power : Byte);
begin
// Now take the number of panels into account
ballSpeed := (power * ballSize * ballPanels) DIV 24;
end;
constructor TFootball.Create(size : Byte; panels : Byte);
begin
Inherited Create(size); // Call the parent constructor first
ballPanels := panels; // Save the passed number of panels
end;
// The form OnCreate implementation
procedure TForm1.FormCreate(Sender: TObject);
var
beachBall : TBall;
soccerBall : TFootball;
begin
// Create our two balls
beachBall := TBall.Create(5);
soccerBall := TFootball.Create(5, 12);
// How fast are they moving at the moment?
ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
// Now kick each ball with a power of 10
beachBall.Kick(10);
soccerBall.Kick(10);
// How fast are they moving now?
ShowMessageFmt('Beach ball is moving at speed : %d',[beachBall.GetSpeed]);
ShowMessageFmt('Soccer ball is moving at speed : %d',[soccerBall.GetSpeed]);
end;
end.
Bookmarks