Questions tagged [getter-setter]

Public mutator methods in object-oriented programming, responsible for changing and returning the value of private class members, thus keeping with the principle of encapsulation.

2157 questions
796
votes
13 answers

Using @property versus getters and setters

What advantages does the @property notation hold over the classic getter+setter? In which specific cases/situations should a programmer choose to use one over the other? With properties: class MyClass(object): @property def my_attr(self): …
Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
655
votes
9 answers

What's the pythonic way to use getters and setters?

I'm doing it like: def set_property(property,value): def get_property(property): or object.property = value value = object.property What's the pythonic way to use getters and setters?
Jorge Guberte
  • 10,464
  • 8
  • 37
  • 56
221
votes
11 answers

Property getters and setters

With this simple class I am getting the compiler warning Attempting to modify/access x within its own setter/getter and when I use it like this: var p: point = Point() p.x = 12 I get an EXC_BAD_ACCESS. How can I do this without explicit backing…
Atomix
  • 13,427
  • 9
  • 38
  • 46
192
votes
8 answers

Instance member cannot be used on type

I have the following class: class ReportView: NSView { var categoriesPerPage = [[Int]]() var numPages: Int = { return categoriesPerPage.count } } Compilation fails with the message: Instance member 'categoriesPerPage' cannot be used on…
Aderstedt
  • 6,301
  • 5
  • 28
  • 44
174
votes
5 answers

Is it possible to implement dynamic getters/setters in JavaScript?

I am aware of how to create getters and setters for properties whose names one already knows, by doing something like this: // A trivial example: function MyObject(val){ this.count = 0; this.value = val; } MyObject.prototype = { get…
daiscog
  • 11,441
  • 6
  • 50
  • 62
168
votes
2 answers

How do you implement a private setter when using an interface?

I've created an interface with some properties. If the interface didn't exist all properties of the class object would be set to { get; private set; } However, this isn't allowed when using an interface,so can this be achieved and if so how?
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103
160
votes
16 answers

Why is it impossible to override a getter-only property and add a setter?

Why is the following C# code not allowed: public abstract class BaseClass { public abstract int Bar { get;} } public class ConcreteClass : BaseClass { public override int Bar { get { return 0; } set {} } } CS0546…
ripper234
  • 222,824
  • 274
  • 634
  • 905
142
votes
9 answers

c#: getter/setter

I saw something like the following somewhere, and was wondering what it meant. I know they are getters and setters, but want to know why the string Type is defined like this. Thanks for helping me. public string Type { get; set; }
Maya
  • 7,053
  • 11
  • 42
  • 53
137
votes
8 answers

Getters and Setters in Kotlin

In Java, for example, I can write getters on my own (generated by IDE) or use Annotations like @Getter in lombok - which was pretty simple. Kotlin however has getters and setters by default. But I can't understand how to use them. I want to make it,…
nutella_eater
  • 3,393
  • 3
  • 27
  • 46
132
votes
10 answers

Is it possible to read the value of a annotation in java?

this is my code: @Column(columnName="firstname") private String firstName; @Column(columnName="lastname") private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { …
BeeS
  • 1,329
  • 2
  • 9
  • 3
114
votes
7 answers

How can I overwrite a getter method in an ActiveRecord model?

I'm trying to overwrite a getter method for an ActiveRecord model. I have an attribute called name in the model Category, and I'd like to be able to do something like this: def name name_trans || name end If name_trans attribute is not nil, then…
oyvindhauge
  • 3,496
  • 2
  • 29
  • 45
106
votes
12 answers

Getters, setters, and properties best practices. Java vs. C#

I'm taking a C# class right now and I'm trying to find out the best way of doing things. I come from a Java background and so I'm only familiar with Java best-practices; I'm a C# novice! In Java if I have a private property, I do this; private…
Vivin Paliath
  • 94,126
  • 40
  • 223
  • 295
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
89
votes
4 answers

Why I can't use let in protocol in Swift?

I have a doubt about protocols in Swift about the use of var and the keywords { get set }. From Apple documentation: If a protocol requires a property to be gettable and settable, that property requirement cannot be fulfilled by a constant stored…
Massimo Polimeni
  • 4,826
  • 4
  • 27
  • 54
88
votes
11 answers

Is it good practice to make getters and setters inline?

public: inline int GetValue() const { return m_nValue; } inline void SetValue(int nNewValue) { this -> m_nValue = nNewValue; } On Learn C++, they said it would run faster. So, I thought it would be great to…
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
1
2 3
99 100