简体   繁体   English

Java代码-如何设置对象变量

[英]Java code - how to set object variable

I am just learning and would like to know about a piece of code that sets the object variable. 我只是在学习,想了解一段设置对象变量的代码。

What is the correct way to set object variable bfield in the follwoing test class? 在后续测试类中设置对象变量bfield的正确方法是什么?

public class test {
private String afield;

private String bfield;

public test() {

buildList();

}

public void buildList() {

    some code to derive and populate afield.

    this.bfield = this.afield;   //  ( 1)

    setBfield(afield);  // (2) say getter and setters do exist

    bfield = afield;  //  (3)
}

What is the right way to do? 正确的做法是什么? I soption 1 OK or option 2? 我选择选项1还是选项2?

setter/getters are more preferable because you can encapsulate some processing in those accessor methods too setter / getter是更可取的,因为您也可以在那些访问器方法中封装一些处理


Also See 另请参阅

Any of the three will work, of course. 当然,这三个都可以。

I generally don't like option 1, unless i'm differentiating between an instance member and an argument. 我通常不喜欢选项1,除非我要区分实例成员和参数。 For example, public void buildList(String bfield) { this.bfield = bfield; } 例如, public void buildList(String bfield) { this.bfield = bfield; } public void buildList(String bfield) { this.bfield = bfield; } . public void buildList(String bfield) { this.bfield = bfield; } this.everything is extra noise; this.everything都是多余的噪音; if you don't need it, all it does is give the bugs more code to hide in. :) 如果您不需要它,它所做的就是给错误提供更多代码来隐藏。:)

Option 2 is more future-proof; 选项2更具前瞻性; if ever you change things so that something else has to be set along with bfield (or if bfield doesn't need a backing field at all -- for instance, if setting it should set something on a sub-object), you'll be glad you called setBfield -- cause you won't have a dozen places to change code that sets bfield . 如果您进行了一些更改,以便必须与bfield一起设置其他bfield (或者bfield根本不需要后备字段,例如,如果设置它应该在子对象上设置某些内容),很高兴你叫setBfield因为你不会有很多地方可以更改设置bfield代码。 Basically, if you need and already have a setBfield method, i'd recommend using it in most cases. 基本上,如果需要并且已经有setBfield方法,我建议在大多数情况下使用它。

If you have a field you know will always be contained within the object itself, and is independent of other fields, option 3 is typically faster. 如果您拥有一个字段,那么您知道该字段将始终包含在对象本身中,并且独立于其他字段,那么选项3通常会更快。 Plus, you don't have to create a setter (read: pollute your interface), if you don't want outside code to be able to set bfield as well. 另外,如果您不希望外部代码也能够设置bfield ,则不必创建setter(请参阅:污染您的界面)。

Use eclipse! 使用蚀! Let it do some work for you. 让它为您做些工作。 Create a class Test like this. 这样创建一个类Test。

public class Test {
   private String afield;
   private String bfield;
}

and then do these: 然后执行以下操作:

  • right click -> select 'Source' -> Generate constructor 右键单击->选择“源”->生成构造函数
  • right click -> select 'Source' -> Generate constructor with fields 右键单击->选择“源”->生成带有字段的构造函数
  • right click -> select 'Source' -> Generate getters / setters 右键单击->选择“源”->生成获取器/设置器

done :) and do lookup for java bean convention. 完成:)并查找Java bean约定。 your code would freak out any java exp dev! 您的代码会吓坏任何Java exp开发人员! :) :)

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

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