accessor

How to avoid getters and setters

Deadly 提交于 2019-12-02 16:41:29
I have read in many places that "getters and setters are evil". And I understood why so. But I don't know how to avoid them completely. Say Item is a class that has information about item name, qty, price etc... and ItemList is a class, which has a list of Items. To find the grand total: int grandTotal() { int total = 0; for (Item item: itemList) total += item.getPrice(); return total; } In the above case, how does one avoid getPrice()? The Item class provides getName, setName, etc.... How do I avoid them? Jonathan Fingland When should you use getters and setters? Getters and setters are great

Intermingling attr_accessor and an initialize method in one class

我与影子孤独终老i 提交于 2019-12-02 14:18:59
I see code like: class Person def initialize(name) @name = name end end I understand this allows me to do things like person = Person.new and to use @name elsewhere in my class like other methods. Then, I saw code like: class Person attr_accessor :name end ... person = Person.new person.name = "David" I'm just at a loss with these two methods mesh. What are the particular uses of def initialize(name) ? I suppose attr_accessor allows me to read and write. That implies they are two separate methods. Yes? Want clarifications on def initialize and attr_accessor and how they mesh. initialize and

How to initialize a trait variable that is a val

梦想与她 提交于 2019-12-02 11:19:10
I have MyObject and MyTrait: class MyObject(private val myname: String = "") extends MyTrait { _name = myname def foo(myname : String) { _name = myname } } trait MyTrait { protected var _name: String = _ def name = _name } This works fine as this val myObject = new MyObject("abc") println(myObject.name) myObject.foo("def") println(myObject.name) prints abc def as expected. Problem now is that I want MyTrait._name to be a val instead of a var. But there is no way I can manage to get this to compile. Any hints appreciated. Regards, Oliver Here is an answer that uses the very latest cutting-edge

for-in loop VS in-operator

一曲冷凌霜 提交于 2019-12-02 07:21:48
问题 I consider myself a JS veteran but just now for the first time I have realised that the for ... in loop does something very different from the in operator: "length" in []; // true for (k in []) { if(k == "length") alert(); }; // k will never be "length" So this brings me to my question: why is the in operator at all present in the for ... in loop? Imho it is totally misleading, as it does different things. Also the notion that first the for operation makes the JS engine take all enumerable

for-in loop VS in-operator

删除回忆录丶 提交于 2019-12-02 06:31:51
I consider myself a JS veteran but just now for the first time I have realised that the for ... in loop does something very different from the in operator: "length" in []; // true for (k in []) { if(k == "length") alert(); }; // k will never be "length" So this brings me to my question: why is the in operator at all present in the for ... in loop? Imho it is totally misleading, as it does different things. Also the notion that first the for operation makes the JS engine take all enumerable properties and then on that subset the in operator is used is imho wrong: simply because the in operator

Do methods which return Reference Types return references or cloned copy?

南楼画角 提交于 2019-12-02 04:47:14
I've been learning Java these days and what I've read just is "Be careful not to write accessor methods that return references to mutable objects" which is really interesting. And now I am wondering whether it is same for Properties and Accessor methods in C#? Or C# already returns cloned copies automatically? Thanks. A reference is just that... a reference to some object that is stored in memory. Unless you explictly write code to create a clone and return a reference to that object, then you will always be passing around a reference to the same instance. The situation it is trying to get you

Set accessor not being called when I deserialise object from Json.net

佐手、 提交于 2019-12-02 01:50:16
public class SpecialObject { public string ID; [JsonIgnore] public List<SpecialObject> SpecialObjectCollection = new List<SpecialObject>(); [JsonIgnore] public List<string> tempObjectIDs = new List<string>(); [JsonProperty] public List<string> SpecialObjectIDs { get { return SpecialObjectCollection.Select(x => x.ID).ToList(); } set { tempObjectIDs = value; } } public SpecialObject() { } public SpecialObject(string _id) { ID = _id; } } static void Main(string[] args) { SpecialObject parent = new SpecialObject("parentIDstring"); parent.SpecialObjectCollection.Add(new SpecialObject("childIDstring

Composing multicast delegates in C# - should I use operators or Action.Combine?

冷暖自知 提交于 2019-12-01 16:46:59
Reading the documentation I can see that + operator can be used to compose/combine delegates of the same type. In the same way I can see that I can remove a from the composed delegate using the - operator. I also noticed that the Action type has static Combine and Remove methods that can be used to concatenate the invocation lists of two delegates, and to remove the last occurrence of the invocation list of a delegate from the invocation list of another delegate respectively. Action a = () => Debug.WriteLine("Invoke a"); Action b = () => Debug.WriteLine("Invoke b"); a += b; a.Invoke(); /

Composing multicast delegates in C# - should I use operators or Action.Combine?

狂风中的少年 提交于 2019-12-01 16:38:30
问题 Reading the documentation I can see that + operator can be used to compose/combine delegates of the same type. In the same way I can see that I can remove a from the composed delegate using the - operator. I also noticed that the Action type has static Combine and Remove methods that can be used to concatenate the invocation lists of two delegates, and to remove the last occurrence of the invocation list of a delegate from the invocation list of another delegate respectively. Action a = () =>

common lisp: slot-value for defstruct structures

孤街浪徒 提交于 2019-12-01 16:21:50
In common lisp, what can I use to access structure slot using slot name/symbol? What I want is (defstruct point (x 0) (y 0)) (defmacro -> (struct slot) `(slot-value ,struct ,slot)) (setf p (make-point)) (setf (slot-value p 'x) 1) (setf (-> p 'y) 2) I'm using clozure cl, and In clozure cl this works. However, AFAIK this is non-standard behavior (equivalent to "undefined behavior" C++). I'm not planning to switch to another CL implementation, so should I keep using slot-value for structures, or is there a better way to do it? Usually you would use accessor functions with structures. Your code