getter

Is object creation in getters bad practice?

会有一股神秘感。 提交于 2019-11-26 20:54:52
问题 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 ? 回答1: Properties look like fields but they are methods. This has been known to cause a phenomenal amount of confusion. When a

C# add validation on a setter method

本秂侑毒 提交于 2019-11-26 20:29:44
问题 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

Difference in C# between different getter styles

限于喜欢 提交于 2019-11-26 19:39:06
问题 I do sometimes see abbreviations in properties for the getter. E.g. those two types: public int Number { get; } = 0 public int Number => 0; Can someone please tell me if there are any differences between those two. How do they behave? Are both of them read-only? 回答1: Yes, both of them are read-only, but there is a difference. In the first one, there's a backing field which is initialized to 0 before the constructor is executed. You can change the value only in the constructor , just like a

Best way of invoking getter by reflection

折月煮酒 提交于 2019-11-26 18:45:15
问题 I need to get the value of a field with a specific annotation, So with reflection I am able to get this Field Object. The problem is that this field will be always private though I know in advance it will always have a getter method. I know that I can use setAccesible(true) and get its value (when there is no PermissionManager), though I prefer to invoke its getter method. I know that I could look for the method by looking for "get+fieldName" (though I know for example for boolean fields are

Lazy getter doesn't work in classes

末鹿安然 提交于 2019-11-26 17:50:29
问题 class a { get b() { delete this.b; return this.b = 1; } } var c = { get b() { delete this.b; return this.b = 1; } } console.log(c.b); // works as expected console.log((new a()).b); // throws error The above code should work fine but the last line throws. Uncaught TypeError: Cannot set property b of # which has only a getter(…) Clearly the getter is not being deleted in class whereas it works fine in object. I am on latest stable chrome. Lazy Getter MDN Entry 回答1: The getter of the class sits

Set and Get Methods in java?

十年热恋 提交于 2019-11-26 17:26:18
How can I use the set and get methods, and why should I use them? Are they really helpful? And also can you give me examples of set and get methods? Set and Get methods are a pattern of data encapsulation. Instead of accessing class member variables directly, you define get methods to access these variables, and set methods to modify them. By encapsulating them in this manner, you have control over the public interface, should you need to change the inner workings of the class in the future. For example, for a member variable: Integer x; You might have methods: Integer getX(){ return x; } void

How to trace a NullPointerException in a chain of getters

南笙酒味 提交于 2019-11-26 16:40:17
问题 If I get a NullPointerException in a call like this: someObject.getSomething().getSomethingElse(). getAnotherThing().getYetAnotherObject().getValue(); I get a rather useless exception text like: Exception in thread "main" java.lang.NullPointerException at package.SomeClass.someMethod(SomeClass.java:12) I find it rather hard to find out wich call actually returend null, often finding myself refactoring the code to something like this: Foo ret1 = someObject.getSomething(); Bar ret2 = ret1

Using @property decorator on dicts

半城伤御伤魂 提交于 2019-11-26 16:32:35
问题 I'm trying to use Python's @property decorator on a dict in a class. The idea is that I want a certain value (call it 'message') to be cleared after it is accessed. But I also want another value (call it 'last_message') to contain the last set message, and keep it until another message is set. In my mind, this code would work: >>> class A(object): ... def __init__(self): ... self._b = {"message": "", ... "last_message": ""} ... @property ... def b(self): ... b = self._b ... self._b["message"]

Java Interface Usage Guidelines — Are getters and setters in an interface bad?

Deadly 提交于 2019-11-26 15:53:17
问题 What do people think of the best guidelines to use in an interface? What should and shouldn't go into an interface? I've heard people say that, as a general rule, an interface must only define behavior and not state. Does this mean that an interface shouldn't contain getters and setters? My opinion: Maybe not so for setters, but sometimes I think that getters are valid to be placed in an interface. This is merely to enforce the implementation classes to implement those getters and so to

Public Data members vs Getters, Setters

老子叫甜甜 提交于 2019-11-26 15:22:26
I am currently working in Qt and so C++. I am having classes that has private data members and public member functions. I have public getters and setters for the data members available in the class. Now my question is, if we have getters and setters for data members in our classes then what's the point in making those data members as private? I agree having private data members in Base classes sounds logical. But besides that, having private members and so do their getters and setters doesn't seem to be of a logical one for me. Or instead can we make all variables as public so that no need for