简体   繁体   English

Java getter / setter字段方法的缩写形式

[英]Abbreviated form of Java getter/setter field methods

Im curious if there exists an abbreviation form for getter/setter methods of objects 我很好奇是否存在对象的getter / setter方法的缩写形式

SimpleObject oSimple = new SimpleObject();
oSimple.setCounterValue(oSimple.getCounterValue() + 1);

like one for simple datatypes 像一个简单的数据类型

int counter = 0;
counter += 2;

Info 信息

The getter/setter methods are required. getter / setter方法是必需的。

Addition 加成

If there isn't a language feature thats support the idea, what is the most convenient way to deal with that in context of good and clean code? 如果没有支持这个想法的语言功能,那么在良好和干净的代码环境中处理它的最方便的方法是什么?

You have C# background I can imagine ;-) 你有C#背景,我可以想象;-)

It's not possible in Java (apart from not-quite-the-same solutions such as having public properties etc). 在Java中是不可能的(除了不完全相同的解决方案,例如拥有公共属性等)。 Same as operator overloading, which could also have solved your issue. 与运算符重载相同,这也可以解决您的问题。

However have a look at http://www.eclipse.org/xtend/ though, it's a JVM language similar to Java that supports operator overloading and some other nifty things. 但是看看http://www.eclipse.org/xtend/ ,它是一种类似于Java的JVM语言,支持运算符重载和其他一些漂亮的东西。

For this case, I create annotation 对于这种情况,我创建了注释

/**
 * Annotation for PropertiesContainer class (that has field without getter and setter)
 * This class look like as class with Properties in C#
 *
 */
public @interface PropertiesContainer {
}

Add add to any class (annotation shows that isn't error) 添加添加到任何类(注释显示不是错误)

@PropertiesContainer // class without getter and setter 
public Class SomeObject{
     public int counter;
}

And just use: 并且只需使用:

oSimple.counter++;

As geert3 said there is no shortcut to setters/getters in Java without accessing the property directly. 正如geert3所说,没有直接访问属性的Java中的setter / getters的快捷方式。

In your case your SimpleObject -class should just have a method increaseCounter() and maybe increaseCounterBy(int add) (or simply add(int a) ). 在您的情况下,您的SimpleObject -class应该只有一个方法increaseCounter()increaseCounterBy(int add) (或简单地add(int a) )。

If the value doesn't have to be checked for any wrong values / execute code when setting it, you could solve it by just referencing the variable directly: 如果该value没有被检查是否有错误的值设置时/执行代码,你可以通过只直接引用变量解决这个问题:

public Class SomeObject{
     public int value;
}

public Class Main{

     public static void main(String[] args){
          SomeObject o = new SomeObject();
          o.value += 1;
     }
}

If the example you gave is all you want to do (increment and assign an index for a new object every time you make one) then a common practice is use a static variable as a counter and a local variable for the specific index. 如果您给出的示例是您想要做的所有(每次创建一个新增对象并为其分配索引),那么通常的做法是使用静态变量作为计数器,使用特定索引的局部变量。

class SomeObject {

    static int objectCounter;
    private int index;

    public SomeObject() {
       index = objectCounter++;
       // additional constructor code
    }
}

If you wish to add a number to a variable, I'd consider making a separate method that does just that rather than trying to rewrite set(), or if it makes sense, make it part of the code for the set(). 如果你想为变量添加一个数字,我会考虑制作一个单独的方法,而不是尝试重写set(),或者如果它有意义,将它作为set()的代码的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM