Questions tagged [getter]

A getter is a public accessor method, used in object-oriented programming, which returns the value associated with a private member of a class.

A getter is public method, used in object-oriented programming, which returns the value associated with a private member of a class. A getter often refers to the get portion of a property. The getter method may perform other tasks such as validation, but is usually considered non-mutating.

1882 questions
43
votes
1 answer

How to override setter in Swift

A Superclass : class MySuperView : UIView{ var aProperty ; } A subclass inheritance the super class : class Subclass : MySuperClass{ // I want to override the aProperty's setter/getter method } I want to override the superclass's…
user5465320
  • 719
  • 2
  • 7
  • 15
42
votes
11 answers

Java Interface Usage Guidelines -- Are getters and setters in an interface bad?

What do people think of the best guidelines to use in an interface? What should and shouldn't go into an interface? I've heard people say that, as a general rule, an interface must only define behavior and not state. Does this mean that an…
His
  • 5,891
  • 15
  • 61
  • 82
40
votes
4 answers

JSR 303 Bean Validation - Why on getter and not setter?

I don't understand why JSR 303 (bean validation) is for the getter methods and not setter? Isn't it more logical to put it under setter method since that is the entry point into a field and validation should be checked prior to that?
yapkm01
  • 3,590
  • 7
  • 37
  • 62
40
votes
20 answers

Java method naming conventions: Too many getters

Why do Java method names use the "get" prefix so extensively? At least in my Java programs there are a lot of methods with names starting with the word "get". The percentage of get-methods is suspiciously high. I am starting to feel that the word…
Are Husby
  • 2,089
  • 2
  • 16
  • 14
39
votes
12 answers

Getter/setter on javascript array?

Is there a way to get a get/set behaviour on an array? I imagine something like this: var arr = ['one', 'two', 'three']; var _arr = new Array(); for (var i = 0; i < arr.length; i++) { arr[i].__defineGetter__('value', function (index) { …
Martin Hansen
  • 5,154
  • 3
  • 32
  • 53
38
votes
8 answers

Create a Setter Only in Swift

When I create a setter such as: var masterFrame: CGRect { set { _imageView.frame = newValue _scrollView.frame = newValue } } It's forcing me to make a getter, which I don't want to do. Is there any way to create a setter in…
Aggressor
  • 13,323
  • 24
  • 103
  • 182
35
votes
3 answers

Passing Argument to JavaScript Object Getter

var URIController = { get href() { return url.location.href; } } I have above object structure. But URIController.href property depends on another object, url. If the url is defined globally, URIController.href works. But I want to…
John Bernard
  • 777
  • 2
  • 7
  • 16
35
votes
12 answers

Jackson Not Overriding Getter with @JsonProperty

JsonProperty isn't overriding the default name jackson gets from the getter. If I serialize the class below with ObjectMapper and jackson I get {"hi":"hello"} As you can see the JsonProperty annotation has no effect class JacksonTester { String…
tt_Gantz
  • 2,786
  • 3
  • 23
  • 43
35
votes
1 answer

Python overriding getter without setter

class human(object): def __init__(self, name=''): self.name = name @property def name(self): return self._name @name.setter def name(self, value): self._name = value class superhuman(human): …
LuRsT
  • 3,973
  • 9
  • 39
  • 51
34
votes
1 answer

Lambda expression for setter

We have lambda expression for getter as below: Function studentNameGetter = Student::getName; How about lambda expression for the setter?
Kowser
  • 8,123
  • 7
  • 40
  • 63
34
votes
4 answers

MVVM: Should a VM object expose an M object directly, or only through getters delegating to M's getters?

the best way to explain is with example so: this is the model public class Person { public int age; public string name; } this is the view model public class PersonVM { } my question is: should the vm expose the person to the data…
Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141
33
votes
2 answers

Can JS have getters and setters methods named same as property?

I want to achieve behaviour like down in code: function Foo(name) { this.name = name; }; var myFoo = new Foo('myName'); myFoo.name('newMyName'); // sets myFoo.name = 'newMyName' myFoo.name(); // returns 'myName' But it is obvious that in that…
Alan Kis
  • 1,790
  • 4
  • 24
  • 47
33
votes
3 answers

Please explain Getter and Setters in Objective C

Possible Duplicate: Setters and Getters (Noobie) - iPhone SDK I am a beginner here. I have just started learning iOS for the last two months and I do not have any programming background. (Little bit of Java though). Can anyone please explain what…
GenieWanted
  • 4,473
  • 4
  • 24
  • 35
31
votes
3 answers

How to exclude getter only properties from type in typescript

Getters in the class are readonly properties so throwing type error from following code make sense. class Car { engine: number; get hp() { return this.engine / 2; } get kw() { return this.engine * 2; …
Eduard Jacko
  • 1,974
  • 1
  • 16
  • 29
31
votes
4 answers

'this' argument has type const but function is not marked const

Okay so I'm a bit of a noob at C++ and in my second assignment I am required to make classes with public and private arguments etc, etc. Basically the mutator functions won't work because apparently they're not of type const? This is the header file…
Liam George
  • 343
  • 1
  • 3
  • 5