getter-setter

dynamic getter and setters - a possibility

为君一笑 提交于 2019-12-07 03:34:00
问题 I am trying to solve a problem that came to my mind lately. Let's say we would want and would know how to make a point in having dynamic getters and setters in javascript, more like those in php (__get, __set). But as javascript does not have a catch-all property the only thing we could do is to provide a list of possible keys and iterate to add getters and setters on those only, and hope none other will ever come. But the problem is not by far solved. So the next approach that came to my

Using value wrapper and operator() overloading to simplify getter/setter design : a dangerous practice?

心已入冬 提交于 2019-12-07 03:23:46
问题 Consider the following class: class MyClass1 { public: double x() const {return _x;} // getter double y() const {return _y;} // getter double z() const {return _x*_y;} // getter void x(const double var) {_x = var;} // setter void y(const double var) {_y = var;} // setter void z(const double var) {_x = var; _y = 1;} // setter protected: double _x; double _y; }; As the actual contents of MyClass1 is an implementation detail, the getters and setters provide a unified way to get and set the class

override getter only needs @synthesize

自古美人都是妖i 提交于 2019-12-07 01:09:23
问题 I want to ovveride getter for lazy instantiation and leave default setter. Do I need @synthesize ? Why ? @interface Foo() @property (strong, nonatomic) NSObject *bar; @end @implementation Foo - (NSObject *)bar { if(!_bar) _bar = [[NSObject alloc] init]; return _bar; } @end Update: I've changed variable and class name, because it was confusing. From Deck and card to Foo and bar. 回答1: No, you only need to explicitly synthesize (to get the synthesized ivar) if you explicitly implement all of the

JAXB edit List getter?

让人想犯罪 __ 提交于 2019-12-06 21:36:36
问题 I have my data model in the form of XSD files from which I then generate the corresponding Java files from xjc using command line. When I generate JAXB classes from an XSD, List type elements gets a getter method generated for them (with no corresponding setter method), as follows: public List<Type> getElement3() { if (element3 == null) { element3 = new ArrayList<Type>(); } return this.element3; } I have lot of fields in almost every file generated from XSD of List type. USE case: Now, I don

How to conform to a protocol's variables' set & get?

删除回忆录丶 提交于 2019-12-06 19:01:13
问题 I'm playing around with protocols and how to conform to them. protocol Human { var height: Int {get set} } struct boy : Human { var height: Int {return 5} // error! } I'm trying to learn different ways that I can implement set and get. However the code above throws the following error: type 'boy' does not conform to protocol 'Human' However writing as below won't have any errors: struct boy : Human { var height = 5 // no error } I don't understand the difference nor what exactly needs to be

“Special attributes/properties” instead of getter/setter in Java to avoid boiler plate code

牧云@^-^@ 提交于 2019-12-06 16:03:15
Intro I am working on an open source project Treez where I organize so called "Atoms" in a tree view. Those Atoms sometimes have a lot of attributes and those attributes are modified either through user actions in a tree view or through an API in an Eclipse code editor. The attributes of my Atoms themselves are represented by reusable "AttributeAtoms" . Those hold the actual attribute value and provide additional functionality like validation (other possible terms for "Atom" might be "widget", "bean", "property" or "tree node"). Question(s) In the past I provided a getter/setter pair for each

Java: how to make a private field Map immutable within a class?

元气小坏坏 提交于 2019-12-06 14:29:06
public class Hi { private final Map<String, String> map; public Map<String, String> getMap() { return map; } } I have this Hi class, and I want map to be immutable. I also need a getter. Currently, another class can modify the map from the getter. I would like to return a copy of the map to solve this problem, but Map is an interface, so does that mean I have to make the getter call: return new HashMap<String,String>(map); Is there another way to do it without forcing the map to be a hashmap? I would like for it to remain the same class as before. Return Collections.unmodifiableMap(map) ,

Why do we use 'this' in setter method but not in getter method? [duplicate]

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 11:53:57
问题 This question already has answers here : What is the meaning of “this” in Java? (19 answers) Closed 2 years ago . For example, in the following code: private int id; public void setID(int ID) { this.id = ID; } public void getID() { return id; } Why don't we say return this.id in the getter function or conversely say id = ID in the setter function? Also is this actually necessary? I mean, aren't the functions called through an object, say obj.setid(1) or obj.getid() ? Will it work differently

Python property with public getter and private setter

孤人 提交于 2019-12-06 04:55:26
I have a python property like this: class Foo: @property def maxInputs(self): return self._persistentMaxInputs.value @maxInputs.setter def maxInputs(self, value): self._persistentMaxInputs.value = value Currently, the value of maxInputs can be get and set by everyone. However, I want to allow everyone to get the value of the maxInputs , but it should only be set inside of the Foo class. So is there a way to declare a property with a private setter and a public getter? Python has no privacy model . Using underscores is only a convention, there is no access control. If you don't want the 'public

Doctrine 2 ORM creates classes with hateful CamelCase

百般思念 提交于 2019-12-06 00:50:11
I created yaml configuration for Doctrine. When I'm trying doctrine orm:generate-entities , it creates php files with getters and setters in camel case. So, is_public field transforms into setIsPublic and getIsPublic methods. It's owful. How can I get set_is_public and get_is_public ? I can manually edit generated php files, but I don't know what will happen when I change the schema. You can choose a naming strategy that Doctrine will use to generate the items using: Using a naming strategy you can provide rules for automatically generating database identifiers, columns and tables names when