getter-setter

Using getter/setter vs “tell, don't ask”?

大憨熊 提交于 2019-12-03 04:20:12
Tell, don't ask principle here is often pasted to me when I use getters or setters, and people tell me not to use them. The site clearly explains what I should and what I shouldn't do, but it doesn't really explain WHY I should tell, instead of asking. I find using getters and setters much more efficient, and I can do more with them. Imagine a class Warrior with attributes health and armor : class Warrior { unsigned int m_health; unsigned int m_armor; }; Now someone attacks my warrior with a special attack that reduces his armor for 5 seconds. Using setter's it would be like this: void

Generate getters and setters in NetBeans [closed]

为君一笑 提交于 2019-12-03 02:54:48
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . I Need to know how to have NetBeans generate getters and setters using a shortcut. 回答1: Position the cursor inside the class, then press ALT + Ins and select Getters and Setters from the contextual menu. 来源: https://stackoverflow.com/questions/21074402/generate-getters-and-setters-in-netbeans

Immutable Object with ArrayList member variable - why can this variable be changed?

半世苍凉 提交于 2019-12-03 02:25:48
问题 I have got one class with various member variables. There is a constructor and there are getter-methods, but no setter-methods. In fact, this object should be immutable. public class Example { private ArrayList<String> list; } Now I noticed the following: when I get the variable list with a getter-method, I can add new values and so on - I can change the ArrayList . When I call the next time get() for this variable, the changed ArrayList is returned. How can this be? I didn't set it again, I

Get Getter Function in Javascript

微笑、不失礼 提交于 2019-12-02 22:55:00
In JavaScript there is the possibility to create getters and setters the following way: function MyClass(){ var MyField; this.__defineGetter__("MyField",function(){ return MyField; }); this.__defineSetter__("MyField",function(value){ MyField = value; }); } But is there a way to get the Getter or Setter FUNCTION? I think of something like this: var obj = new MyClass(); obj.__getSetter__("MyField")("MyValue"); I need such a functionality when extending base classes. For example: Class "A" has field "a", Class "B" extends from "A" and also wants to have a field "a". To pass values from the "a"

Getting error while dealing with getter and setter in kotlin

我是研究僧i 提交于 2019-12-02 21:19:23
问题 I have define the data class as: data class chatModel(var context:Context?) { var chatManger:ChatManager?=null //getter get() = chatManger //setter set(value) { /* execute setter logic */ chatManger = value } } Now how will i access the get() and set() function. In java I do like that: //for getter new chatModel().getJId() //for setter new chatModel().setJId("jid") edit: As the @yole suggested. I am using setter and getter as: //set the data var chatDetails:chatModel=chatModel

Object.defineProperty for all browsers?

若如初见. 提交于 2019-12-02 20:32:29
Asking about Object.defineProperty as demonstrated below: function testComponent(){ var testProperty; Object.defineProperty(this, "testProperty", { get : function() { return testProperty; }, set : function(val) { testProperty = val; } }); } Where it would be used like so: testObject = new testComponent(); testObject.testProperty = "testValue"; Based on what I've seen so far, it looks like there is no cross browser solution, as I've tried using es5-shim with no luck, but I would like to confirm. I also found a reference to this post and my tests still fail in IE 7 & 8, can anyone shed any light

Perform business logic after getters are called

耗尽温柔 提交于 2019-12-02 16:58:46
问题 I would like to write my business logic after the getters and setters are called (twice), because I use their object values inside the business logic. However Construct, Post construct, actionevents,.. are called before the getters. So how can I use the values of the getters if I don't want to write business logic inside them? 回答1: I want to navigate to the site and get data from a database displayed into outputText. Do the job in (post)constructor of the bean. @ManagedBean @RequestScoped

Immutable Object with ArrayList member variable - why can this variable be changed?

与世无争的帅哥 提交于 2019-12-02 15:56:47
I have got one class with various member variables. There is a constructor and there are getter-methods, but no setter-methods. In fact, this object should be immutable. public class Example { private ArrayList<String> list; } Now I noticed the following: when I get the variable list with a getter-method, I can add new values and so on - I can change the ArrayList . When I call the next time get() for this variable, the changed ArrayList is returned. How can this be? I didn't set it again, I just worked on it! With a String this behaviour isn't possible. So what is the difference here? Just

Getter and Setters not working?

混江龙づ霸主 提交于 2019-12-02 14:08:38
问题 I've got two classes right now: RemindersDAO.java and ViewLocalReminders.java. I'm trying to get access to a variable that's in ViewLocalReminders.java and I'm trying to call it from RemindersDAO.java. I'm doing this by using the getter/setter method combo. However, for some reason, my variable value keeps getting set to 0 in the getter method. Here's the code: ViewLocalReminders.java public class ViewLocalReminders extends SherlockListActivity { private long reminderID; public void onCreate