getter-setter

IntelliJ getter/setter format

独自空忆成欢 提交于 2019-11-30 17:00:44
How can you get IntelliJ to generate getter/setters on one line like this: public String getAbc() { return abc; } Instead of public String getAbc() { return abc; } ?? David Robles There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA. Source You can see that in this IntelliJ Wishlist Alex G I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour. Just use the "Generate..." option, or use Alt + Insert shortcut, and select "Getter and Setter". In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top.

Confused with Java Encapsulation Concept

风流意气都作罢 提交于 2019-11-30 15:54:31
问题 Good day! I am reading a Java book about encapsulation and it mentioned the getter and setter method. I've read that to hide the attributes, I must mark my instance variables as "PRIVATE" and make a "PUBLIC" method of getter and setter to access the data. So I tried making a similar but not the conventional code about it as follows: public class AddressBookEntry { private String name; private String address; private String telNo; private String email; public void getAllInfo() { name =

Confused with Java Encapsulation Concept

ぐ巨炮叔叔 提交于 2019-11-30 15:26:27
Good day! I am reading a Java book about encapsulation and it mentioned the getter and setter method. I've read that to hide the attributes, I must mark my instance variables as "PRIVATE" and make a "PUBLIC" method of getter and setter to access the data. So I tried making a similar but not the conventional code about it as follows: public class AddressBookEntry { private String name; private String address; private String telNo; private String email; public void getAllInfo() { name = JOptionPane.showInputDialog("Enter Name: "); address = JOptionPane.showInputDialog("Enter Address: "); telNo =

Error accessing generated ivars when I override setters and getters in Modern Objective-C

一个人想着一个人 提交于 2019-11-30 14:42:28
问题 I know now the new Objective-C compiler lets you not need to synthesize your properties anymore. I have one file that has two classes in it. My .h for a simple helper class looks like this: @interface ViewFrameModel : NSObject @property (nonatomic, strong) UIView *view; @property (nonatomic, assign) CGRect frame; - (id)initWithView:(UIView *)view frame:(CGRect)frame; @end In the same .h file, for my other class (class 2), I have: @property (nonatomic, strong) ViewFrameModel *viewFrameModel;

Why return $this in setter methods?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 11:33:39
Examining Zend Framework, I found that all setter methods (of those I’ve examined) return the instance of the class it lives in. It doesn't only set a value but also returns $this . For example: /* Zend_Controller_Router */ public function setGlobalParam($name, $value) { $this->_globalParams[$name] = $value; return $this; } /* Zend_Controller_Request */ public function setBaseUrl($baseUrl = null) { // ... some code here ... $this->_baseUrl = rtrim($baseUrl, '/'); return $this; } /* Zend_Controller_Action */ public function setFrontController(Zend_Controller_Front $front) { $this->

Angular 2 templates methods vs getters

你离开我真会死。 提交于 2019-11-30 11:16:54
I'm wondering if there is any benefit to do this: <div>{{getSomething()}}</div> export class MyComp { getSomething() { return ...} } Over this: <div>{{getSomething}}</div> export class MyComp { get getSomething() { return ...} } Use methods vs getters to display calculated data. I looked deeper into this and played with typescript Playground. I declared two classes one with getter and the second with get method as described in your questions. Lets see how it looks like: In the first example we declared a method for getting the property value in the following way: class Greeter { greeting:

getters and setters style

 ̄綄美尐妖づ 提交于 2019-11-30 10:57:13
(Leaving aside the question of should you have them at all.) I have always preferred to just use function overloading to give you the same name for both getter and setters. int rate() { return _rate; } void rate(int value) { _rate = value; } // instead of int getRate() { return _rate; } void setRate(int value) { _rate = value; } // mainly because it allows me to write the much cleaner total( period() * rate() ); // instead of setTotal( getPeriod() * getRate() ); Naturally I am correct, but i wondered if the library writers had any good reason ? I would prefer the get/set versions because it is

Why am I allowed to set a read only property of a protocol using a struct that inherits said protocol?

点点圈 提交于 2019-11-30 09:47:54
问题 I'm following a tutorial on the protocol oriented programming paradigm in which I'm confused by something I thought was rather simple which is read only properties of protocols or getters and setters. My understanding is that a read only property is signified by using the keyword 'get' when declaring a variable within a protocol. I was excited so I quickly coded created a playground to see if my thinking was accurate however it appears that I can still change the property which I thought was

How does PHP avoid infinite recursion here?

寵の児 提交于 2019-11-30 09:01:18
Consider this class: class test { public function __set($n, $v) { echo "__set() called\n"; $this->other_set($n, $v, true); } public function other_set($name, $value) { echo "other_set() called\n"; $this->$name = $value; } public function t() { $this->t = true; } } I am overloading PHP's magic __set() method. Whenever I set a property in an object of the test class, it will call __set() , which in turn calls other_set() . $obj = new test; $test->prop = 10; /* prints the following */ __set() called other_set() called But other_set() has the following line $this->$name = $value . Shouldn't this

Why do people write private-field getters returning a non-const reference?

喜欢而已 提交于 2019-11-30 08:02:22
问题 We can all agree on public variables being bad for encapsulation and all that. However, I noticed a lot of code that does this type of stuff: class foo { private: int integer_; string someString_; // other variables public: int& integer() { return integer_; } string& someString() { return someString_; } // other "functions" } int main() { foo f; f.integer() = 10; f.someString() = "something"; return 0; } I have seen this being used in many places and I don't get why. Basically it returns a