Property (programming)

From Wikipedia, the free encyclopedia

In some object-oriented programming languages, a property is a special sort of class member, intermediate between a field (or data member) and a method. You read and write a property just as you read and write a field, but this is (usually) translated to get and set method calls. The field-like syntax is easier to read and write than lots of method calls, yet the interposition of method calls allows for data validation, active updating (as of GUI visuals), and/or read-only 'fields'. That is, properties are intermediate between member code (methods) and member data (instance variables) of the class, and properties provide a higher level of encapsulation than public fields.

Contents

[edit] Support in languages

Programming languages that support properties include Delphi (programming language), Visual Basic and C#. Some object-oriented languages, such as C++ and Java, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.

In most languages, properties are implemented as a pair of accessor/mutator methods, but accessed using the same syntax as for public fields. Omitting a method from the pair yields a read-only or write-only property, the latter being rather uncommon.

In some languages with no built-in support for properties, a similar construct can be implemented as a single method that either returns or changes the underlying data, depending on the context of its invocation. Such techniques are used e.g. in Perl.

[edit] Example syntax

[edit] Delphi

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.)
*)

[edit] Visual Basic

' 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

[edit] 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)"!

[edit] Python

class Pen:
    def __init__(self):
        self._color = 0 # "private" variable
        self._readonly = "Don't Assign to Me!"

    def _set_color(self, color):
        self._color = color

    def _get_color(self):
        return self._color

    color = Property(_get_color, _set_color) # read/write access translates to get/set methods

    def _get_readonly(self):
        return self._readonly

    readonly = Property(_get_readonly) # read-only access is provided (writing throws an exception)

# accessing:
pen = Pen()
# ...
pen.color = ~pen.color # bitwise complement ...

print pen.readonly # this prints "Don't Assign to Me!"
pen.readonly = "Something Else" # this fails (raises an exception)

[edit] See also

In other languages