在Java中,获取器和设置器的约定

17 浏览
0 Comments

在Java中,获取器和设置器的约定

我的Java有点生疏(过去几年一直在写C#)。另外,我希望这不是一个非常主观的问题。\n无论如何,假设我有一个类Person(是的,有点陈词滥调,我知道),没有行为(C#版本):\n

public class Person 
{
   public string Name { get; set; }
   public int Age { get; set; }
   // 假设还有10多个属性
}

\n等价的Java版本会是什么样子?\n我知道我可以写一堆getter和setter(但是假设我有10多个属性),感觉很多重复代码。\n只写这样的代码是不是被认为是不好的实践:\n

public class Person {
   public String name;
   public int age;
   // 这里写其它属性
}

\n我对此感到有些不安。我意识到没有“正确答案”,但我更关心一般的约定和最佳实践。

0
0 Comments

在Java中,常见的一种编码规范是使用getters和setters来访问和修改类的私有字段。这种约定要求开发人员手动创建这些函数来实现对字段的操作。

首先,需要创建一个备份字段(backing field)来存储数据:

private String _backingString;

然后,需要创建getters和setters方法来获取和设置该字段的值:

public String getBackingString() {
  return this._backingString;
}
public void setBackingString(String value) {
  this._backingString = value;
}

这种方式是Java编码规范的一部分,没有其他替代方案。但是,很多集成开发环境(IDE)都提供了自动生成这些方法的工具。可以在你喜欢的IDE中搜索相关的生成工具。

你可以参考这个Stack Overflow上的问题,了解如何在Eclipse中自动生成getters和setters方法:

https://stackoverflow.com/questions/7221691

当我注意到这个问题时,已经进行了修正 🙂

我知道这是个人风格的问题,但是使用下划线(_)作为字段名前缀会让人感到不舒服,而且这也不是标准的做法。

请原谅我作为一个C#开发者的习惯,有时候在Stack Overflow上频繁切换语境时,我的大脑会出现疏忽(比如string和String的别名)。

嗯,我也在使用下划线(_)作为字段名前缀,习惯难以改变。

哦,你是一个C#开发者,那就可以理解了 🙂

你没有遵循Java的编码规范,_variable并不符合规范。

0
0 Comments

在Java中使用getter和setter方法来返回字段的值是更好的做法,这样可以封装字段的计算方式。尽管这种情况很少发生,但有时可能需要更改如下内容:

getBalance()
{
   return this.staticBalance;
}

为:

getBalance()
{
  return calculateBalance();
}

而在Java中,想要改变字段的行为,而不需要改变大量的代码(或者更糟糕的是,需要API用户改变大量的代码)的唯一方法就是使用getter和setter方法。

其他好处包括:

  • 子类可以重写方法的行为
  • 提高线程安全性,因为可以同步访问

编写样板代码很乏味,但是一个好的IDE会根据字段名称为您生成这些方法。

对于解释为什么需要使用setter和getter方法的解释,可以摆脱this.(冗余)。

我将this放在字段上是为了突出它作为字段,因为我不想做一个完整的类定义,但同意在方法调用中这是冗余的,所以从方法调用中去掉了它。

0
0 Comments

Getters and setters in Java convention are used to ensure encapsulation and provide a way to access and modify private fields of a class. Not using getters and setters breaks the encapsulation. In addition, using getters and setters allows for checks or preprocessing before returning or setting the field.

In the provided code snippet, the private field "name" is accessed and modified using a getter and setter. The getter method, "getName()", returns the value of the "name" field, while the setter method, "setName(String name)", sets the value of the "name" field.

Alternatively, the code can be written using annotations from the project Lombok. Lombok is a tool that reduces boilerplate code, and in this case, it can generate the getter and setter methods automatically during compile time.

Using getters and setters not only ensures encapsulation but also provides flexibility in changing the internal representation of the field. If the internal representation needs to be changed, only the getter method needs to be modified, and the clients of the class do not need to worry about the change.

In a discussion, someone suggests using a different naming convention for private fields, such as "name_" instead of "name", and using the setter method to set the value of the private field. This is a personal preference and does not affect the functionality of the code.

Overall, the use of getters and setters in Java convention is important for maintaining encapsulation, providing flexibility, and allowing for checks or preprocessing before accessing or modifying private fields.

0