Difference between revisions of "Property/fr"

From Lazarus wiki
Jump to navigationJump to search
Line 25: Line 25:
 
procedure TCar.SetColor(CarColor: string);
 
procedure TCar.SetColor(CarColor: string);
 
begin
 
begin
   //we can do other things here, too, such as housekeeping, notifying others of changes,
+
   //Nous pouvons faire d'autres choses ici, telles que le ménage, des notification de changement,
 +
  // validition d'autres variables, logging, etc.
 
   //validating against other variables, logging, etc -e.g.
 
   //validating against other variables, logging, etc -e.g.
 
   if CarColor='Pink' then
 
   if CarColor='Pink' then
     ShowMessage('Note: pink is not a very common color for a car.');
+
     ShowMessage('Note: le rose n''est pas une couleur courante pour une voiture.');
 
   FColor:=CarColor;
 
   FColor:=CarColor;
 
end;
 
end;
  
procedure PropertyExample();  //shows how to set properties and read them
+
procedure PropertyExample();  //Montre comment affecter des propriétés et les lires
 
var
 
var
   MyCar: TCar;                // Will hold a TCar instance; used in the example subAuto
+
   MyCar: TCar;                // Une intance de TCar; utilisée dans l'exemple
 
begin
 
begin
   MyCar := TCar.Create;        // Create the object
+
   MyCar := TCar.Create;        // Crée l'objet,
 
   try
 
   try
     MyCar.Color:='Green';      // Sets the color property... which calls the setcolor function... which sets the object's FColor variable
+
     MyCar.Color:='Green';      // Définit la propriété Color... qui appelle la procédure setcolor ... qui affecte la variable FColor.
     showmessage(MyCar.Color);  // Now read the object property again... should show Green
+
     showmessage(MyCar.Color);  // Lit maintenant la propriété Color... doit montrer 'Green'
 
   finally
 
   finally
     MyCar.Free;                // Free the memory for the object again, regardless of what errors occurred in the rest of the code
+
     MyCar.Free;                // Libère la mémoire de l'objet, même si une erreur s'est produite
 
   end;
 
   end;
 
end;
 
end;

Revision as of 11:03, 8 July 2014

Template:property
Retour aux mots réservés.

Documentation

Documentation officielle FPC: [1]

Description

Le mot réservé property fait partie de la programmation orientée objet. Il permet plusieurs niveaux d'accès (read, read/write etc) depuis l'extérieur à des variables dans la classe.

Example

type
  TCar = class
  private
    FColor: string;
    FBuildYear: integer;
    procedure SetColor(CarColor: string);
  public
    property Color: string read FColor write SetColor; //Lit directement la variable FColor 
    // écrit à travers la procédure SetColor qui change la variable FColor.
    // Une autre option est de faire simplement "write FColor" pour changer directement 
    // la variable FColor...
  end;

procedure TCar.SetColor(CarColor: string);
begin
  //Nous pouvons faire d'autres choses ici, telles que le ménage, des notification de changement,
  // validition d'autres variables, logging, etc. 
  //validating against other variables, logging, etc -e.g.
  if CarColor='Pink' then
    ShowMessage('Note: le rose n''est pas une couleur courante pour une voiture.');
  FColor:=CarColor;
end;

procedure PropertyExample();   //Montre comment affecter des propriétés et les lires
var
  MyCar: TCar;                 // Une intance de TCar; utilisée dans l'exemple
begin
  MyCar := TCar.Create;        // Crée l'objet,
  try
    MyCar.Color:='Green';      // Définit la propriété Color... qui appelle la procédure setcolor ... qui affecte la variable FColor.
    showmessage(MyCar.Color);  // Lit maintenant la propriété Color... doit montrer 'Green'
  finally
    MyCar.Free;                // Libère la mémoire de l'objet, même si une erreur s'est produite
  end;
end;

Objects as properties

You can assign objects as properties, e.g.:

type
  TEngine = class
  ...
  TCar = class
  private
    FMyEngine: TEngine;
  public
    property Engine: TEngine read FMyEngine;
  implementation
  ...
  Code in TCar that creates and frees FMyEngine as needed (e.g. in the constructor and destructor)
  ...

Read access here means something that may not be obvious at first glance. Outside code has read only access to the pointer/reference to the TEngine object in question, which means it cannot create or destroy the object.

However, it can use this read access to access the properties of the FMyEngine object and change those.

If you wish to give calling code the responsibility of creating and destroying the object, you can also assign write access, e.g.

...
  public
    property Engine: TEngine read FMyEngine write FMyEngine;
...