Questions tagged [accessor]

An Accessor is (usually) a function that is responsible for reading or writing a property.

Overview

It's a common practice in OOP to make the properties of a class unaccessible from outside.

To provide a way to operate the data in those properties, accessors are made. Typically, two accessors are provided for a property, one for getting the value, and one for setting it. However, the real advantage of accessors is that they can perform some additional computation before the data is actually set or read. For example, if one has a class for dividing numbers, an accessor can prevent setting the divisor property to zero, and act accordingly.

A typical pair of accessors (in C++) (note how we can put checks into the accessors):

class MyClass
{
public:
    void set( int newValue );
    int & get( void );
private:
    int property;
}

void MyClass::set( int newValue )
{
   //we can utilize the accessor to check the value before actually storing it.
   if( newValue > 0 )
       property = newValue;
   else
       return;
}

int & MyClass::get()
{
    return property;
}

See also

866 questions
239
votes
16 answers

Are getters and setters poor design? Contradictory advice seen

I'm currently working on a simple game in Java with several different modes. I've extended a main Game class to put the main logic within the other classes. Despite this, the main game class is still pretty hefty. After taking a quick look at my…
Mike B
  • 12,768
  • 20
  • 83
  • 109
237
votes
18 answers

Is there a way to simulate the C++ 'friend' concept in Java?

I would like to be able to write a Java class in one package which can access non-public methods of a class in another package without having to make it a subclass of the other class. Is this possible?
Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127
132
votes
4 answers

Is it possible to use getters/setters in interface definition?

At the moment, TypeScript does not allow use get/set methods(accessors) in interfaces. For example: interface I { get name():string; } class C implements I { get name():string { return null; } } furthermore, TypeScript…
Ivan Popov
  • 1,331
  • 2
  • 8
  • 5
131
votes
4 answers

What are 'get' and 'set' in Swift?

I'm learning Swift and I'm reading The Swift Programming Language from Apple. I don't have any Objective-C background (only PHP, JavaScript, and others, but not Objective-C). On page 24-25 I see this code: //...Class definition stuff... var…
Mr.Web
  • 6,992
  • 8
  • 51
  • 86
108
votes
21 answers

Accessors are only available when targeting ECMAScript 5 and higher

I am trying to run this code, but it is giving me the following errors: Animal.ts(10,13): error TS1056: Accessors are only available when targeting ECMAScript 5 and higher. Animal.ts(14,13): error TS1056: Accessors are only available when targeting…
jagdish khetre
  • 1,381
  • 2
  • 8
  • 15
104
votes
3 answers

Private setter typescript?

Is there a way to have a private setter for a property in TypeScript? class Test { private _prop: string; public get prop() : string { return this._prop; } private set prop(val: string) { //can put…
sheamus
  • 3,001
  • 4
  • 31
  • 54
85
votes
10 answers

Conventions for accessor methods (getters and setters) in C++

Several questions about accessor methods in C++ have been asked on SO, but none was able satisfy my curiosity on the issue. I try to avoid accessors whenever possible, because, like Stroustrup and other famous programmers, I consider a class with…
Noarth
  • 3,981
  • 6
  • 23
  • 16
82
votes
4 answers

C# Custom getter/setter without private variable

I learned c# recently, so when I learned to write properties, I was taught to do it like this: public string Name { get; set; } Auto properties are great! But now I'm trying to do something a little more complicated, so I need to write a custom…
Tin Can
  • 2,540
  • 2
  • 29
  • 39
81
votes
9 answers

Why can't we assign a foreach iteration variable, whereas we can completely modify it with an accessor?

I was just curious about this: the following code will not compile, because we cannot modify a foreach iteration variable: foreach (var item in MyObjectList) { item = Value; } But the following will compile and…
GianT971
  • 4,385
  • 7
  • 34
  • 46
80
votes
2 answers

Directly accessing an instance variable vs. Using an accessor method

Can anyone explain the difference between accessing an instance attribute via self.attribute and by @attribute?
pistacchio
  • 56,889
  • 107
  • 278
  • 420
74
votes
11 answers

Accessing object property as string and setting its value

I have an instance of the Account class. Each account object has an owner, reference, etc. One way I can access an accounts properties is through accessors like account.Reference; but I would like to be able to access it using dynamic string…
zanona
  • 12,345
  • 25
  • 86
  • 141
45
votes
4 answers

Visual Studio keyboard short-cut to complete default accessors {get; set;}

I am looking for a keyboard short-cut to complete creating the default accessors for a property in a C# class. Something like... I start typing: public int Id Then I press one or more keys, and I endup with: public int Id { get; set; }
Zamboni
  • 7,897
  • 5
  • 43
  • 52
41
votes
1 answer

What are the differences amongst Python's "__get*__" and "_del*__" methods?

I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__…
Zearin
  • 1,474
  • 2
  • 17
  • 36
36
votes
4 answers

Why Automatically implemented properties must define both get and set accessors

When we define a property like public string Name {get; set;} dot net can make our properties code. but when we use public string Name {get;} public string Name {set;} we face with 'Hajloo.SomeThing.PropertyName.set' must declare a…
Nasser Hadjloo
  • 12,312
  • 15
  • 69
  • 100
35
votes
4 answers

Using reflection to set an object property

I getting class by name and i need to update them with respective data and my question is how to do it with java I want to add the method some dummy data . I don't know the class type I just getting the class name and use reflection to get his data…
Stefan Strooves
  • 624
  • 2
  • 10
  • 16
1
2 3
57 58