A property, in some object-oriented programming languages, is a special sort of class member, intermediate in functionality between a field (or data member) and a method. The syntax for reading and writing of properties is like for fields, but property reads and writes are (usually) 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 (e.g., of GUI elements), or implementation of what may be called "read-only fields".
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++, do not support properties, requiring the programmer to define a pair of accessor and mutator methods instead.[1]
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++ does not have first class properties, they can be emulated with operator overloading.[2]
Also note that some C++ compilers support first class properties as language extensions.
__declspec(property)
creates properties similar to C#.__property
keyword.[5]In many object oriented 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 (Ruby, Smalltalk) achieve property-like syntax using normal methods, sometimes with a limited amount of syntactic sugar.
Some languages follow well-established syntax conventions for formally specifying and utilizing properties and methods.
Among these conventions:
The following example demonstrates dot notation in JavaScript.
The following example demonstrates bracket notation in JavaScript.
// 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.
C++ does not have first class properties, but there exist several ways to emulate properties to a limited degree. Two of which follow:
template
struct Foo ;
struct Bar ;
int main Also see Stack Overflow for a more detailed example.
An example taken from the MSDN documentation page.
int main
// the set property can also be used in expressions, just like regular assignmentint theColor = (pen.color = 0xFF0000);
In D version 2, each property accessor or mutator must be marked with @property:
function TPen.GetColor: TColor;begin Result := FColor;end;
procedure TPen.SetColor(const AValue: TColor);begin if FColor <> AValue then FColor := AValue;end;
(*Delphi and Free Pascal also support a 'direct field' syntax -
property Color: TColor read FColor write SetColor;
or
property Color: TColor read GetColor write FColor;
where the compiler generates the exact same code as for reading and writinga 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 memberaccess with a method call.)
member this.Color with get = _color and set value = _color <- valueend
@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:
Properties only work correctly for new-style classes (classes that have object
as a superclass), and are only available in Python 2.2 and newer (see the relevant section of the tutorial Unifying types and classes in Python 2.2). Python 2.6 added a new syntax involving decorators for defining properties.
@property def color(self): return self._color
@color.setter def color(self, color): self._color = color
pen.color = ~pen.color # Bitwise complement ...
# Defines a setter for the @color field def color=(value) @color = value endend
pen = Pen.newpen.color = ~pen.color # Bitwise complement
Ruby also provides automatic getter/setter synthesizers defined as instance methods of Class.
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 endend
pen = Pen.newputs pen.brand # Accesses the pen brand through the generated getterpen.size = 0.5 # Updates the size field of the pen through the generated setterpen.color = ~pen.color
Public Property Color As Integer ' Public property Get Return _color End Get Set(ByVal value As Integer) _color = value End Set End Property
End Class
' Set valuepen.Color = 1
' Get valueDim color As Int32 = pen.Color
Public Property Color As Integer ' Public property
End Class
' Set valuepen.Color = 1
' Get valueDim color As Int32 = pen.Color
Public Property Get Color As Long Color = m_ColorEnd Property
Public Property Let Color(ByVal RHS As Long) m_Color = RHSEnd Property