getter-setter

Most appropriate place for bounds checking - constructor or setter?

孤者浪人 提交于 2019-11-30 07:42:50
问题 Still relatively new to Java and I'm wondering which is the better way to handle this. I have a class constructor that takes a few parameters, and also in this class are public getters and setters: private String name; private Float value; public MySampleClass(String theName, Float theValue) { setName(theName); setValue(theValue); } public void setName(String n) { this.name = n; } public value setValue(Float v) { this.value = v; } I'd like to do some bounds checking on this Float. It seems

Generating Entity Getters and Setters in Symfony / Doctrine ORM

拥有回忆 提交于 2019-11-30 06:34:34
I have the following ORM Symfony entity with only properties : <?php namespace Evr\HomeBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Table(name="ev_article") * @ORM\Entity */ class Article { /** * * @ORM\Column(name="article_id", type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * * @ORM\ManyToOne(targetEntity="Subategory",inversedBy="articles") * @ORM\JoinColumn(name="subcategory_id",referencedColumnName="id") */ private $subcategory; /** * * @ORM\Column(type="string",length=512) */ private $title; /** * * @ORM\Column(type="text") */ private

Is it bad practice to have my getter method change the stored value?

大憨熊 提交于 2019-11-30 04:09:13
Is it bad practice to change my getter method like version 2 in my class. Version 1: public String getMyValue(){ return this.myValue } Version 2: public String getMyValue(){ if(this.myValue == null || this.myValue.isEmpty()){ this.myValue = "N/A"; } return this.myValue; } I think it is actually quite a bad practice if your getter methods change the internal state of the object. To achieve the same I would suggest just returning the "N/A" . Generally speaking this internal field might be used in other places (internally) for which you don't need to use the getter method. So in the end, the call

Getters and setters for arrays

独自空忆成欢 提交于 2019-11-30 02:37:30
问题 I have a few questions about getters and setters for arrays. Suppose we have a class like this, which makes a private copy of an array in its constructor: import java.util.Arrays; public class Foo { private int[] array; public Foo(int[] array) { this.array = Arrays.copyOf(array, array.length); } } We want the array to only be accessed/mutated via the getters and setters. If we have a getter that looks like this: public int[] getArray() { return array; } it defeats the purpose of the getter as

Is there a way to intercept setters and getters in C#?

好久不见. 提交于 2019-11-30 00:54:07
问题 In both Ruby and PHP (and I guess other languages as well) there are some utility methods that are called whenever a property is set. ( *instance_variable_set* for Ruby, *__set* for PHP). So, let's say I have a C# class like this: public class Person { public string FirstName { get; set; } public string LastName { get; set; } } Now, let's say that if any property setter from the Person class is called, I want to call another method first, and then continue with the default behaviour of the

Cross-browser Getter and Setter

别说谁变了你拦得住时间么 提交于 2019-11-29 21:49:04
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. 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 function. I think in IE8 you may be able to with DOM elements, but I don't believe it works for regular native

How do you implement a private setter when using an interface?

不想你离开。 提交于 2019-11-29 20:54:22
I've created an interface with some properties. If the interface didn't exist all properties of the class object would be set to { get; private set; } However, this isn't allowed when using an interface,so can this be achieved and if so how? In interface you can define only getter for your property interface IFoo { string Name { get; } } However, in your class you can extend it to have a private setter - class Foo : IFoo { public string Name { get; private set; } } Interface defines public API. If public API contains only getter, then you define only getter in interface: public interface IBar

How can I overwrite a getter method in an ActiveRecord model?

╄→гoц情女王★ 提交于 2019-11-29 20:47:41
I'm trying to overwrite a getter method for an ActiveRecord model. I have an attribute called name in the model Category , and I'd like to be able to do something like this: def name name_trans || name end If name_trans attribute is not nil, then return it, else return name attribute. How would I do this? This should then be called normally like this: @category.name Wonsup Lee The Rails Style Guide recommends using self[:attr] over read_attribute(:attr) . You can use it like this: def name name_trans || self[:name] end meagar Update: The preferred method according to the Rails Style Guide is

What are the differences amongst Python's “__get*__” and “_del*__” methods?

浪尽此生 提交于 2019-11-29 20:12:00
I just started learning Python a few months ago, and I'm trying to understand the differences between the different __get*__ methods: __get__ __getattr__ __getattribute__ __getitem___ And their __del*__ equivalents: __del__ __delattr__ __delete__ __delitem__ What are the differences between these? When should I use one over the other? Is there a specific reason why most of the __get*__ methods have __set*__ equivalents, but there is no __setattribute__ ? Rik Poggi The documentation for every method that you listed is easly reachable from the documentation index . Anyway this may be a little

Do you use the get/set pattern (in Python)?

旧时模样 提交于 2019-11-29 19:15:13
Using get/set seems to be a common practice in Java (for various reasons), but I hardly see Python code that uses this. Why do you use or avoid get/set methods in Python? Cool link: Python is not Java :) In Java, you have to use getters and setters because using public fields gives you no opportunity to go back and change your mind later to using getters and setters. So in Java, you might as well get the chore out of the way up front. In Python, this is silly, because you can start with a normal attribute and change your mind at any time, without affecting any clients of the class. So, don't