- Propriété (informatique)
-
Pour les articles homonymes, voir Propriété (homonymie).
En programmation orientée objet ou en RDF, une propriété est un élément de description d'un objet. Un type de classe regroupe, entre autres, l'ensemble des propriétés communes à un type d'objet.
Une propriété, dans certains langages de programmation orientés objet, est une sorte de membre de classe. Il est un champ du point de vue de l'interface d'une classe mais il est développé comme une méthode qui va lire et modifier le contenu de l'instance. Son usage courant est de pouvoir rajouter des instructions au moment de la modification ou de la lecture d'une instance d'une classe sans pour autant modifier l'interface de cette classe.
Sommaire
Exemple de syntaxe
C#
class Pen { private int m_Color; // private field public int Color { // public property get { return m_Color; } set { m_Color = value; } } }
// accessing: Pen pen = new Pen(); // ... pen.Color = ~pen.Color; // bitwise complement ... // another silly example: pen.Color += 1; // a lot clearer than "pen.set_Color(pen.get_Color() + 1)"!
Recent C# versions also allow "auto-implemented properties" where the backing field for the property is generated by the compiler during compilation. This means that the property must have a setter, however it can be private.
class Shape { public Int32 Height { get; set; } public Int32 Width { get; private set; } }
C++
C++ n'a pas de propriétés de classe, mais il existe plusieurs manières d'imiter les propriétés jusqu'à une certaine limite. Voici deux exemple :
#include <iostream> template <typename T> class property { T value; public: T & operator = (const T &i) { ::std::cout << i << ::std::endl; return value = i; } // This template class member function template serves the purpose to make // typing more strict. Assignment to this is only possible with exact identical // types. template <typename T2> T2 & operator = (const T2 &i) { ::std::cout << "T2: " << i << ::std::endl; T2 &guard = value; throw guard; // Never reached. } operator T const & () const { return value; } }; struct Foo { // Properties using unnamed classes. class { int value; public: int & operator = (const int &i) { return value = i; } operator int () const { return value; } } alpha; class { float value; public: float & operator = (const float &f) { return value = f; } operator float () const { return value; } } bravo; }; struct Bar { // Using the property<>-template. property <bool> alpha; property <unsigned int> bravo; }; int main () { Foo foo; foo.alpha = 5; foo.bravo = 5.132f; Bar bar; bar.alpha = true; bar.bravo = true; // This line will yield a compile time error // due to the guard template member function. ::std::cout << foo.alpha << ", " << foo.bravo << ", " << bar.alpha << ", " << bar.bravo << ::std::endl; return 0; }
C++, Microsoft & C++Builder-specific
Un exemple tiré de la MSDN documentation page.
// declspec_property.cpp struct S { int i; void putprop(int j) { i = j; } int getprop() { return i; } __declspec(property(get = getprop, put = putprop)) int the_prop; }; int main() { S s; s.the_prop = 5; return s.the_prop; }
D
class Pen { private int m_color; // private field // public get property public int color () { return m_color; } // public set property public int color (int value) { return m_color = value; } }
auto pen = new Pen; pen.color = ~pen.color; // bitwise complement // the set property can also be used in expressions, just like regular assignment int theColor = (pen.color = 0xFF0000);
Dans D version 2, chaque accesseur de propriété doit être marqué avec @property:
class Pen { private int m_color; // private field // public get property @property public int color () { return m_color; } // public set property @property public int color (int value) { return m_color = value; } }
Delphi/Free Pascal
type TPen = class private m_Color: Integer; function Get_Color: Integer; procedure Set_Color(RHS: Integer); public property Color: Integer read Get_Color write Set_Color; end; function TPen.Get_Color: Integer; begin Result := m_Color end; procedure TPen.Set_Color(RHS: Integer); begin m_Color := RHS end;
// accessing: var pen: TPen; // ... pen.Color := not pen.Color; (* Delphi also supports a 'direct field' syntax - property Color: Integer read m_Color write Set_Color; or property Color: Integer read Get_Color write m_Color; where the compiler generates the exact same code as for reading and writing a field. This offers the efficiency of a field, with the safety of a property. (You can't get a pointer to the property, and you can always replace the member access with a method call.) *)
F#
type Pen() = class let mutable _color = 0 member this.Color with get() = _color and set value = _color <- value end
let pen = new Pen() pen.Color <- ~~~pen.Color
JavaScript
function Pen() { this._color = 0; } // Add the property to the Pen type itself, can also // be set on the instance individually Object.defineProperties(Pen.prototype, { color: { get: function () { return this._color; }, set: function (value) { this._color = value; } } });
var pen = new Pen(); pen.color = ~pen.color; // bitwise complement pen.color += 1; // Add one
Objective-C 2.0
@interface Pen : NSObject { NSColor *color; } @property(copy) NSColor *color; // color values always copied. @end @implementation Pen @synthesize color; // synthesize accessor methods. @end // Example Usage Pen *pen = [[Pen alloc] init]; pen.color = [NSColor blackColor]; float red = pen.color.redComponent; [pen.color drawSwatchInRect:NSMakeRect(0, 0, 100, 100)];
Notez que le moderne Objective-C runtime peut convertir les variables d'instance en propriétés, d'où la déclaration explicite des variables d'instance qui n'est pas nécessaire, mais toujours possible.
PHP
class Pen { private $_color; function __set($property, $value) { if ($property == 'Color') { return $this->_color = $value; } } function __get($property) { if ($property == 'Color') { return $this->_color; } } }
$p = new Pen(); $p->Color = ~$p->Color; // bitwise complement echo $p->Color;
Python
Les propriétés ne fonctionne correctement que pour les nouveaux types de classe qui sont uniquement disponibles en Python 2.2 ou plus récents. Python 2.6 ajoute une nouvelle syntaxe pour définir les propriétés.
class Pen(object): def __init__(self): self._color = 0 # "private" variable @property def color(self): return self._color @color.setter def color(self, color): self._color = color
pen = Pen() # accessing: pen.color = ~pen.color # bitwise complement ...
Ruby
class Pen def initialize @color = 0 end # there is actually a shortcut for these: attr_accessor :color will # synthetise both methods automatically, it was expanded for # compatibility with properties actually worth writing out def color @color end def color = value @color = value end end pen = Pen.new pen.color = ~pen.color
Visual Basic
Visual Basic (.NET to 2010)
Public Class Pen Private m_Color As Integer ' Private field Public Property Color As Integer ' Public property Get Return m_Color End Get Set(ByVal Value As Integer) m_Color = Value End Set End Property End Class
' accessing: Dim pen As New Pen() ' ... pen.Color = Not pen.Color
Visual Basic 6
' in a class named clsPen Private m_Color As Long Public Property Get Color() As Long Color = m_Color End Property Public Property Let Color(ByVal RHS As Long) m_Color = RHS End Property
' accessing: Dim pen As New clsPen ' ... pen.Color = Not pen.Color
Voir aussi
Wikimedia Foundation. 2010.