All Topics  
Property (programming)

 

   Email Print
   Bookmark   Link






 

Property (programming)



 
 
In some object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s, a property is a special sort of class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 member, intermediate between a field
Field (computer science)

In computer science, data that has several parts can be divided into fields. For example, a computer may represent today's date as three distinct fields: the day, the month and the year....
 (or data member) and a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls. The field-like syntax is said to be 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'.






Discussion
Ask a question about 'Property (programming)'
Start a new discussion about 'Property (programming)'
Answer questions from other users
Full Discussion Forum



Encyclopedia


In some object-oriented
Object-oriented programming

Object-oriented programming is a programming paradigm that uses "Object_" and their interactions to design applications and computer programs....
 programming language
Programming language

A programming language is a machine-readable artificial language designed to express computations that can be performed by a machine, particularly a computer....
s, a property is a special sort of class
Class (computer science)

In object-oriented programming, a class is a programming language construct that is used as a blueprint to create Object s. This blueprint includes Attribute s and Method s that the created objects all share....
 member, intermediate between a field
Field (computer science)

In computer science, data that has several parts can be divided into fields. For example, a computer may represent today's date as three distinct fields: the day, the month and the year....
 (or data member) and a method
Method (computer science)

In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
. Properties are read and written like fields, but property reads and writes are (usually) translated to get and set method calls. The field-like syntax is said to be 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 variable
Instance variable

In object-oriented programming with Class es, an instance variable is a variable defined in a class, for which each object in the class has a separate copy....
s) of the class, and properties provide a higher level of encapsulation
Information hiding

Information hiding in computer science is the principle of hiding of design decisions in a computer program that are most likely to change, thus protecting other parts of the program from change if the design decision is changed....
 than public field
Field (computer science)

In computer science, data that has several parts can be divided into fields. For example, a computer may represent today's date as three distinct fields: the day, the month and the year....
s.

Support in languages


Programming languages that support properties include Delphi/Free Pascal
Free Pascal

Free Pascal is a free software, Portability , open source, Pascal programming language and Object Pascal compiler. The 32/64-bit multi-CPU architecture and cross-platform compiler implements the Borland Pascal programming language dialects as well as some MacPascal constructs, and is available for...
, Visual Basic
Visual Basic

'Visual Basic' is the third-generation programming language event-driven programming and integrated integrated development environment from Microsoft for its Component Object Model programming model....
, C#, D
D (programming language)

The D programming language, also known simply as D, is an Object-oriented programming, Imperative programming, Multi-paradigm programming language system programming language by Walter Bright of Digital Mars....
, eC, Objective C 2.0, Python and Vala
Vala (programming language)

Vala is a programming language that tries to bring modern language features to C , without additional runtime requirements and with little overhead, by targeting the GObject object system....
. Some object-oriented languages, such as Java
Java (programming language)

Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java ....
, don't support properties, and require the programmer to define a pair of accessor and mutator
Mutator method

In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation ....
 methods instead. While C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 doesn't have first class properties, they can be emulated due to operator overloading.

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
Perl

In computer programming, Perl is a high-level programming language, List of programming languages by category, Interpreter , dynamic programming language....
.

Example syntax


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


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

Visual Basic (.NET to 2008)

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

C#

class Pen



// 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)"!

C++

C++
C++

C++ is a general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level programming language and low-level programming language language features....
 does not have first class properties, but there exist several ways to emulate properties. One of which is as follows:

  1. include


template class property ;

struct Foo ;

struct Bar ;

int main

Dev 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 = (pen.color = 0xFF0000);

Python


Properties only work correctly for new-style classes (classes which has object as an ancestor
Ancestor

An ancestor is a parent or the parent of an ancestor .Two individuals have a genetics relationship if one is the ancestor of the other, or if they share a common ancestor....
), and are only available in Python 2.2 and newer (see ).

class Pen(object): def __init__(self): self.__color = 0 # "private" variable self.__writeonly = "You can't read this!"

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 _set_writeonly(self, new_value): self.__writeonly = new_value

writeonly = property(fset = _set_writeonly) # write-only access is provided (reading throws an exception)

pen = Pen
  1. accessing:
pen.color = ~pen.color # bitwise complement ...

print pen.writeonly # raise "AttributeError: unreadable attribute" pen.writeonly = "Something Else" # __writeonly is now "Something Else" print pen._Pen__writeonly

PHP

class Pen

$p = new Pen; $p->Color = !$p->Color; echo $p->Color;

eC

class Pen

// Example Usage Pen pen ; Pen pen ; pen.color = ~pen.color; pen.color += 10; pen.color.r = 255; pen.color = 0xFF0000; pen.color = ; pen.color = ColorHSV ; pen.color = ColorLab ; pen.color = ColorCMYK ;

Objective C 2.0

@interface Pen : NSObject @property(copy) NSColor *color;// color values always copied. @end

@implementation Pen @synthesize color; // synthesize accessor methods. @end

// Example Usage Pen *pen = [Pen new]; pen.color = [NSColor blackColor]; float red = pen.color.redComponent; [pen.color drawSwatchInRect:NSMakeRect(0, 0, 100, 100)];

See also

  • Bound property
    Bound property

    A bound property of an object is a Property which transmits notification of any changes to an adapter or event handler.A simply example is the text property of a textbox control....
  • Uniform access principle
    Uniform access principle

    The Uniform Access Principle was put forth by Bertrand Meyer. It states "All services offered by a module should be available through a uniform notation, which does not betray whether they are implemented through storage or through computation." This principle applies generally to object-oriented programming languages....
  • Method (computer science)
    Method (computer science)

    In object-oriented programming, a method is a subroutine that is exclusively associated either with a class or with an object . Like a procedure in procedural programming languages, a method usually consists of a sequence of statement to perform an action, a set of input parameter to customize those actions, and possibly an output value...
  • Mutator method
    Mutator method

    In computer science, a mutator method is a method used to control changes to a variable.The mutator method, sometimes called a "setter", is most often used in object-oriented programming, in keeping with the principle of encapsulation ....
  • Field (computer science)
    Field (computer science)

    In computer science, data that has several parts can be divided into fields. For example, a computer may represent today's date as three distinct fields: the day, the month and the year....