Property (programming)
A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are translated to 'getter' and 'setter' method calls. The field-like syntax is easier to read and write than many method calls, yet the interposition of method calls "under the hood" allows for data validation, active updating, or implementation of what may be called " fields".
See an instructive example for C# language below.
Support in languages
Programming languages that support properties include ActionScript 3, C#, D, Delphi/Free Pascal, eC, F#, Kotlin, JavaScript, Objective-C 2.0, Python, Scala, Swift, Lua, and Visual Basic.Some object-oriented languages, such as Java and C++, don't support properties, and require the programmer to define a pair of accessor and mutator methods instead.
Oberon-2 provides an alternative mechanism using object variable visibility flags.
Other languages designed for the Java Virtual Machine, such as Groovy, natively support properties.
While C++ doesn't have first class properties, they can be emulated due to operator overloading.
Also note that some C++ compilers support first class properties.
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 an uncommon write-only property.
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.
Some languages achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.
Syntax variants
Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.Among these conventions:
- Dot notation
- Bracket notation
Dot notation
document.createElement;
Bracket notation
The following example demonstrates bracket notation in JavaScript.document;
Example syntax
C#
class Pen
// accessing:
Pen pen = new Pen;
int color_tmp = 0;
//...
pen.Color = 17;
color_tmp = pen.Color;
//...
pen.Color = ~pen.Color; // bitwise complement...
// another silly example:
pen.Color += 1; // a lot clearer than "pen.set_Color"!
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
C++
does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:- include
struct Foo ;
struct Bar ;
int main
C++, Microsoft & C++Builder-specific
An example taken from the MSDN .// declspec_property.cpp
struct S
int main
D
class Pen
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 = ;
In D version 2, each property accessor or mutator must be marked with @property:
class Pen
Delphi/Free Pascal
type TPen = class
private
FColor: TColor;
function GetColor: TColor;
procedure SetColor;
public
property Color: Integer read GetColor write SetColor;
end;
function TPen.GetColor: TColor;
begin
Result := FColor;
end;
procedure TPen.SetColor;
begin
if FColor <> AValue
then FColor := AValue;
end;
// accessing:
var Pen: TPen;
//...
Pen.Color := not Pen.Color;
eC
Pen blackPen ;
Pen whitePen ;
Pen pen3 ;
Pen pen4 ;
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
// Add the property to the Pen type itself, can also
// be set on the instance individually
Object.defineProperties;
var pen = new Pen;
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // Add one
ActionScript 3.0
package
var pen:Pen = new Pen;
pen.color = ~pen.color; // bitwise complement
pen.color += 1; // add one
Objective-C 2.0
@interface Pen : NSObject
@property NSColor *colour; // The "copy" attribute causes the object's copy to be
// retained, instead of the original.
@end
@implementation Pen
@synthesize colour; // Compiler directive to synthesise accessor methods.
// It can be left behind in Xcode 4.5 and later.
@end
The above example could be used in an arbitrary method like this:
Pen *pen = Pen alloc] init];
pen.colour = ;
float red = pen.colour.redComponent;
;
PHP
class Pen
$p = new Pen;
$p->color = ~$p->color; // Bitwise complement
echo $p->color;
Python
Properties only work correctly for new-style classes, and are only available in Python 2.2 and newer. Python 2.6 added a new syntax involving decorators for defining properties.class Pen:
def __init__ -> None:
self._color = 0 # "private" variable
@property
def color:
return self._color
@color.setter
def color:
self._color = color
pen = Pen
- Accessing:
Ruby
class Pen
def initialize
@color = 0
end
# Defines a getter for the @color field
def color
@color
end
# Defines a setter for the @color field
def color=
@color = value
end
end
pen = Pen.new
pen.color = ~pen.color # Bitwise complement
Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.
class Pen
attr_reader :brand # Generates a getter for @brand
attr_writer :size # Generates a setter for @size
attr_accessor :color # Generates both a getter and setter for @color
def initialize
@color = 0 # Within the object, we can access the instance variable directly
@brand = "Penbrand"
@size = 0.7 # But we could also use the setter method defined by the attr_accessor Class instance method
end
end
pen = Pen.new
puts pen.brand # Accesses the pen brand through the generated getter
pen.size = 0.5 # Updates the size field of the pen through the generated setter
pen.color = ~pen.color
Visual Basic
Visual Basic (.NET 2003-2010)
Public Class Pen
Private _color As Integer ' Private field
Public Property Color As Integer ' Public property
Get
Return _color
End Get
Set
_color = value
End Set
End Property
End Class
' Create Pen class instance
Dim pen As New Pen
' Set value
pen.Color = 1
' Get value
Dim color As Int32 = pen.Color
Visual Basic (only .NET 2010)
Public Class Pen
Public Property Color As Integer ' Public property
End Class
' Create Pen class instance
Dim pen As New Pen
' Set value
pen.Color = 1
' Get value
Dim color As Int32 = 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
m_Color = RHS
End Property
' accessing:
Dim pen As New clsPen
'...
pen.Color = Not pen.Color