getter

Using getters within class methods

守給你的承諾、 提交于 2019-11-28 23:13:21
If you have a class with some plain get/set properties, is there any reason to use the getters within the class methods, or should you just use the private member variables? I think there could be more of an argument over setters (validation logic?), but I'm wondering just about getters. For example (in Java) - is there any reason to use option 2?: public class Something { private int messageId; public int getMessageId() { return this.messageId; } public void setMessage(int messageId) { this.messageId = messageId; } public void doSomething() { // Option 1: doSomethingWithMessageId(messageId);

Java :Setter Getter and constructor

回眸只為那壹抹淺笑 提交于 2019-11-28 21:51:10
问题 I'm a bit confused about the use of getter/setters and constructors (see the below code for an example) public class ExampleClass { private int value = 0; public ExampleClass () { value = 0; } public ExampleClass (int i) { this.value = i; } public int getValue() { return value; } public void setValue(int val) { this.value = val; } public static void main(String[] args) { ExampleClass example = new ExampleClass (20); example.setValue(20); //Both lines above do same thing - why use constructor?

C# add validation on a setter method

人盡茶涼 提交于 2019-11-28 20:53:50
I have a a couple of variables that i define in C# by: public String firstName { get; set; } public String lastName { get; set; } public String organization { get; set; } What i want is to add validation to these methods when you try to set a value. Lets say your going to set a value for firstName, the i should pass thrue a regexp to actuelly be set, otherwise an exception should be thrown. Is this possible to build with this "short syntax" or should I go for standard (like in JAVA) getters and setters and in there validate the data? jason If you want to validate when the property is set, you

What should a C++ getter return

泄露秘密 提交于 2019-11-28 18:09:54
What is the best practice for a C++ getter method which is supposed to return a non trivial type, but a member which is of type class, or struct. Return by value, such as: MyType MyClass::getMyType() { return mMyType; } Return by const reference: const MyType& MyClass::getMyType() { return mMyType; } Return by address: MyType* MyClass::getMyType() { return &mMyType; } where class MyType { /* ... */ }; class MyClass { private: MyType mMyType; } I specifically worry about the following usages of this method. Can you please elaborate in details how this might affect copying the object, and the

Cross-browser Getter and Setter

一个人想着一个人 提交于 2019-11-28 17:38:09
问题 This works in modern Chrome/Firefox/Opera but fails in IE8. Haven't tried it in IE9. How can I make this cross-browser compatible, including IE7+? (Fiddle here.) var foo = { get test(){ return 'Works'; } }; // foo.test should be 'Works' I've seen some usage with __defineGetter__ but that threw an 'unrecognized method' error in IE8. 回答1: I don't believe you can. In IE8 and lower, property access is mere property access. There's no way to run function code without explicitly invoking the

Good or bad practice? Initializing objects in getter

岁酱吖の 提交于 2019-11-28 15:00:35
I have a strange habit it seems... according to my co-worker at least. We've been working on a small project together. The way I wrote the classes is (simplified example): [Serializable()] public class Foo { public Foo() { } private Bar _bar; public Bar Bar { get { if (_bar == null) _bar = new Bar(); return _bar; } set { _bar = value; } } } So, basically, I only initialize any field when a getter is called and the field is still null. I figured this would reduce overload by not initializing any properties that aren't used anywhere. ETA: The reason I did this is that my class has several

What is the difference between _name and self.name if name was a NSString

删除回忆录丶 提交于 2019-11-28 12:44:33
问题 What is the defference if I called NSString *theNameToDisplay = _name; or NSString *theNameToDisplay = self.name; I know it might be a silly question but I see both versions used a lot and I don't spot a difference in the output? Thanks! 回答1: Assume you have in your .m file this line (and don't have any overriden methods to direct access to _name) @synthesize name = _name; It mean that property name (self.name) will use variable _name when you try to access it. In this case self.name is equal

How does EL #{bean.id} call managed bean method bean.getId()

非 Y 不嫁゛ 提交于 2019-11-28 11:50:14
I do not really understand how getter and setter work althougth it is a basic concept. I have the following code, how is the attribute id sent to Managed Bean? Is it captured by getter method? My facelet <p:inputText id="id" value="#{bean.id}"> My managed bean private String id; public void setId(String id) { this.id = id; } public String getId() { return id; } Luiggi Mendoza The call of getter and setter methods by #{} expressions is not part of JSF but Expression Language (most known as EL). JSF takes advantage of EL to bind the data of the HTML components to the fields of a bean through

Swift subscript with different signature for the getter and setter

扶醉桌前 提交于 2019-11-28 10:55:05
问题 Is it possible to have a subscript in Swift that has different signatures for the getter and setter ? For example, I want the getter to return a Set<Int> and the setter to take an Int (not a Set<Int> ). This code won't compile but it gives you an idea of what I'm trying to do: struct Foo{ subscript(index: Int)->String{ get{ return "bar" // returns a String } set(newValue: String?){ // takes a String? instead of a String print(newValue) } } } How can I do this? 回答1: This is very ugly, and I

Is object creation in getters bad practice?

与世无争的帅哥 提交于 2019-11-28 08:58:21
Let's have an object created in a getter like this : public class Class1 { public string Id { get; set; } public string Oz { get; set; } public string Poznamka { get; set; } public Object object { get { // maybe some more code return new Object { Id = Id, poznamla = Poznamka, Oz = OZ }; } } } Or should I rather create a Method that will create and return the object ? Sergey Teplyakov Properties look like fields but they are methods. This has been known to cause a phenomenal amount of confusion. When a programmer sees code that appears to be accessing a field, there are many assumptions that