getter-setter

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

前提是你 提交于 2019-12-03 14:33:40
问题 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

Getters and Setters for ArrayLists in Java

谁都会走 提交于 2019-12-03 13:27:36
问题 How would I go about using getters and setters for an ArrayList in Java? Here is what I have. I think I am using the getter correctly, but I don't know how to use a setter for an ArrayList. private ArrayList<String> stringlist = new ArrayList<String>(); public ArrayList<String> getStringList() { return stringlist; } public ArrayList<String> setStringList() { // I don't know what to put here // I also don't know if my return value is correct } 回答1: ArrayList is a mutable container class,

Use of getter-setter within class

走远了吗. 提交于 2019-12-03 12:36:06
Should one use under any circumstances getters-setters of a class within the class? Jigar Joshi Getters setters are generally used from outside class from inside directly access the fields. The main advantage/purpose is encapsulation of getter setters, If your getters setters has some logical code then use it. for example : public void setValue(int val){ if(val > 100) this.val = 0; else this.val = val; } Also see why-use-getters-and-setters More on getters and setters Yes, getters and setters are useful. Because PHP doesn't support type hinting for simple types like int or string, you cannot

C# getter and setter shorthand

人盡茶涼 提交于 2019-12-03 11:40:38
If my understanding of the internal workings of this line is correct: public int MyInt { get; set; } Then it behind the scenes does this: private int _MyInt { get; set; } Public int MyInt { get{return _MyInt;} set{_MyInt = value;} } What I really need is: private bool IsDirty { get; set; } private int _MyInt { get; set; } Public int MyInt { get{return _MyInt;} set{_MyInt = value; IsDirty = true;} } But I would like to write it something like: private bool IsDirty { get; set; } public int MyInt { get; set{this = value; IsDirty = true;} } Which does not work. The thing is some of the objects I

JavaWorld on OO: Getters/Setters vs Builder

て烟熏妆下的殇ゞ 提交于 2019-12-03 10:19:53
问题 Background: I found this article on JavaWorld, where Allen Holub explains an alternative to Getters/Setters that maintains the principle that the implementation of an object should be hidden (his example code can also be found below). It is explained that the classes Name / EmployeeId / Money should have a constructor taking a single string - the reasoning is that if you type it as an int , and later need to change it to a long , you will have to modify all the uses of the class, and with

Get Getter Function in Javascript

佐手、 提交于 2019-12-03 08:54:15
问题 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

Override all setters and getters of a subclass

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 08:07:30
I want to override the setter and getter and find the class of an objc_property_t without doing it individually for each property. I get all the properties like so: unsigned int numberOfProperties; objc_property_t *propertyArray = class_copyPropertyList([self class], &numberOfProperties); for (NSUInteger i = 0; i < numberOfProperties; i++) { objc_property_t property = propertyArray[i]; NSString *name = [[NSString alloc] initWithUTF8String:property_getName(property)]; property.getter = SEL; //? } This is an example of how I want to override the getter and setter - if there is a better way, let

Overriding Doctrine_Record (sfDoctrineRecord) instance methods in Doctrine PHP Symfony

让人想犯罪 __ 提交于 2019-12-03 07:49:24
My background is in Propel, so I was hoping it would be a simple thing to override a magical getter in a Doctrine_Record (sfDoctrineRecord), but I'm getting either a Segfault or the override method is simply ignored in favor of the one in the superclass. https://gist.github.com/697008eaf4d7b606286a class FaqCategory extends BaseFaqCategory { public function __toString() { return $this->getCategory(); } // doesn't work // override getDisplayName to fall back to category name if getDisplayName doesn't exist public function getDisplayName() { // also tried parent::getDisplayName() but got

When to use get/set Methods in java [duplicate]

与世无争的帅哥 提交于 2019-12-03 05:40:56
问题 This question already has answers here : Why use getters and setters/accessors? (38 answers) Closed 3 years ago . I want to know when to use get and set methods(getName,setName ) in my class and when simple classVariable.name = "" instead а = classVariable.getName() Here is example of class using set and get methods public class ClassExampe { String name; String course; public String getName ( ) { return name; } public void setName (String studentName) { name = studentName; } public String

Independent getter/setter methods, or combined?

本小妞迷上赌 提交于 2019-12-03 05:00:26
While working on a project, I've been making some changes and browsing around existing framework API docs for insight. While perusing the Kohana docs, I noticed that the getters/setters of any given class are typically combined: public function someProperty($value = null){ if(is_null($value){ return $this->_someProperty; } $this->_someProperty = $value; return $this; } Rather than: public function setSomeProperty($value){ $this->_someProperty = $value; return $this; } public function getSomeProperty(){ return $this->_someProperty; } Is there any value in doing this ( the former ), beyond