6

关于我的域对象中生成的 getter 和 setter 方法,我有一个小问题。我想为我的源代码使用通用样式指南。该样式指南的一部分说我以成员前缀“m”开头每个类成员名称。

class User{
String mName;
List<Call> mAllCall;
List<Geo> mAllGeo;

不幸的是,我有几个具有更多成员变量的类。我遇到的问题是我是一个非常懒惰的开发人员,我在 Eclipse 中创建了 getter 和 setter 方法

“源”->“生成 Getter 和 Setter”。

结果是

public String getmName() {
    return mName;
}
public void setmName(String mName) {
    this.mName = mName;
}
public List<Call> getmAllCall() {
    return mAllCall;
}
public void setmAllCall(List<Call> mAllCall) {
    this.mAllCall = mAllCall;
}
public List<Geo> getAllGeo() {
    return mAllGeo;
}
public void setmAllGeo(List<Geo> mAllGeo) {
    this.mAllGeo = mAllGeo;
}

那不是我想要的结果。我需要这个:

public String getName() {
    return mName;
}
public void setName(String pName) {
    this.mName = pName;
}
public List<Call> getAllCall() {
    return mAllCall;
}
public void setAllCall(List<Call> pAllCall) {
    this.mAllCall = pAllCall;
}
public List<Geo> getAllGeo() {
    return mAllGeo;
}
public void setmAllGeo(List<Geo> pAllGeo) {
    this.mAllGeo = mAllGeo;
}

我目前手动删除并替换方法名称中的前缀。有没有更简单的方法来做到这一点?

4

3 回答 3

24

For the prefix m, you add the letter m to your list of prefixes in the Java Code Style.

Follow these steps:

  1. open Preferences,
  2. in left panel, expand Java,
  3. expand Code Style,
  4. right panel is where you should now be looking at

You will see a list with Fields, Static Fields, etc. This is what you need to modify.

Set m against Fields.

Set p against the Parameter.

As the name of the field will now be different from the name of the argument, the this. qualification will no longer be added automatically. However, you can check the option Qualify all generated field accesses with 'this.' to have it again.

I suppose that you know the difference between Enable project specific settings and Configure Workspace Settings... in the upper left and right of the window?

于 2013-01-03T20:06:24.563 回答
3

我根本不喜欢这个主意,但是..

您可以编写不带前缀的成员m,让 Eclipse 创建 getter 和 setter,然后重命名成员(Shift-Alt-R);Eclipse 将更改引用,但不会(除非您明确告诉它)getters/setters 签名。

于 2013-01-03T17:30:43.597 回答
1

getter 和 setter 方法的名称派生自字段名称。如果您对字段使用前缀或后缀(例如 fValue、_value、val_m),您可以在代码样式首选项页面(Windows > 首选项 > Java > 代码样式)中指定后缀和前缀。

参考这里

于 2013-08-25T10:06:28.510 回答