• Aucun résultat trouvé

A Class Example

Dans le document Teach Yourself Borland Delphi 4 in 21 Days (Page 90-95)

Right now it would be nice if you could see an example of a class. Listing 3.1 shows a unit that contains a class called TAirplane. This class could be used by an

aircraft controller program. The class enables you to command an airplane by

sending it messages. You can tell the airplane to take off, to land, or to change its course, altitude, or speed. First take a look at the unit and then I'll discuss what is going on within this class.

procedure TakeOff(Dir : Integer); virtual;

procedure Land; virtual;

public

constructor Create(AName : string; AKind : Integer = Airliner);

function SendMessage(Msg : Integer; var Response : string;

Spd : Integer; Dir : Integer; Alt : Integer) : Boolean;

function GetStatus(var StatusString : string) : Integer; overload; Âvirtual;

function GetStatus : Integer; overload;

function GetSpeed : Integer;

function GetHeading : Integer;

function GetAltitude : Integer;

function GetName : string;

end;

implementation

constructor TAirplane.Create(AName : string; AKind : Integer);

begin

function TAirplane.SendMessage(Msg : Integer; var Response : string;

Spd : Integer; Dir : Integer; Alt : Integer) : Boolean;

if Response <> `' then begin

function TAirplane.GetStatus(var StatusString : string) : Integer;

begin

StatusString := Format(`%s, Altitude: %d, Heading: %d, ` + `Speed: %d', [Name, Altitude, Heading, Speed]);

Result := Status;

function TAirplane.GetAltitude : Integer;

begin

Result := Altitude;

end;

function TAirplane.GetName : string;

begin

Result := Name;

end;

end.

ANALYSIS: Look first at the class declaration in the interface section. Notice that the TAirplane class has one overloaded function called GetStatus. When called with a string parameter, GetStatus will return a status string as well as the status data member (the string parameter is a variable parameter). When called without a parameter, it just returns Status. Note that the only way to access the private fields is via the public methods. For example, you can change the speed, altitude, and heading of an airplane only by sending it a message. To use an analogy, consider that an air traffic controller cannot physically change an aircraft's heading. The best he can do is send a message to the pilot and tell him to change to a new heading.

NOTE: This class would benefit greatly from properties. As I said earlier, I'm not ready to discuss properties in detail at this point in the book, so I'll have to admit that this class could be much better than it is and move on.

Now turn your attention to the definition of the TAirplane class in the interface section. The constructor performs some initialization chores. You have probably noticed that the SendMessage function does most of the work. A case statement determines which message was sent and takes the appropriate action. Notice that the TakeOff and Land procedures cannot be called directly (they are protected) but rather are called through the SendMessage function. Again, as an air traffic controller, you can't make an aircraft take off or land, you can only send it a message telling it what you want it to do.

There's something else here that I haven't discussed yet. Note the virtual keyword. This specifies that the function is a virtual method.

New Term: A virtual method is a method that is automatically called if a method of that name exists in the derived class.

I'll discuss virtual methods in the next section, but I wanted to point them out to you now.

The book's code contains a program called Airport, which enables you to play air traffic controller. (You can find the book's code at the Web site http://www.mcp.com/info. Type in the book's ISBN: 0-672-31286-7.) The program first sets up an array of TAirplane classes and then creates three instances of the TAirplane class. You can send messages to any airplane by selecting the airplane, setting up the parameters for the message, and then clicking the Execute button. Clicking the button results in a call to the selected airplane's SendMessage function. When you send a message, you get a response back from the airplane, and that response is displayed in a memo component. Run the program and play with it to get a feel for how it works.

Figure 3.1 shows the Airport program running.

Inheritance

One of the most powerful features of classes in Object Pascal is that they can be extended through inheritance.

New Term: Inheritance means taking an existing class and adding functionality by deriving a new class from it.

New Term: The class you start with is called the base class or ancestor class, and

the new class you create is called the derived class.

To illustrate, let's go back to the TAirplane class. The civilian and military worlds are quite different, as you know. To represent a military aircraft, I can derive a class from TAirplane and add functionality to it:

TMilitaryPlane = class(TAirplane) private

TheMission : TMission;

constructor Create(AName : string; AType : Integer);

function GetStatus(var StatusString : string) : Integer; override;

protected

procedure TakeOff(Dir : Integer); override;

procedure Land; override;

procedure Attack; virtual;

procedure SetMission; virtual;

end;

A TMilitaryPlane has everything a TAirplane has, plus a few more goodies. Note the first line of the class declaration. The class name in parentheses after the class keyword is used to tell the compiler that I am inheriting from another class. The class from which I am deriving this class is the base class and, in this case, is the TAirplane class.

NOTE: When you derive a class from another class, the new class gets all the functionality of the base class plus whatever new features you add. You can add fields and methods to the new class, but you cannot remove anything from what the base class offers.

You'll notice that in the private section there is a line that declares a variable of the TMission class. The TMission class is not shown here, but it could encapsulate everything that deals with the mission of a military aircraft: the target, navigation

waypoints, ingress and egress altitudes and headings, and so on. This illustrates the use of a field that is an instance of another class. In fact, you'll see that a lot when programming in Delphi.

Dans le document Teach Yourself Borland Delphi 4 in 21 Days (Page 90-95)