在类方法中访问类属性的好习惯是什么?
我总是想知道从 Java 中的类方法访问类属性的最佳方法。
您能否快速说服我以下 3 个解决方案中的哪一个(或完全不同的一个 :P)是一个好的做法?
public class Test {
String a;
public String getA(){
return this.a;
}
public setA(String a){
this.a = a;
}
// Using Getter
public void display(){
// Solution 1
System.out.println(this.a);
// Solution 2
System.out.println(getA());
// Solution 3
System.out.println(this.getA());
}
// Using Setter
public void myMethod(String b, String c){
// Solution 1
this.a = b + c;
// Solution 2
setA(b + c);
// Solution 3
this.setA(b + c);
}
}
I always wonder about the best way to access a class attribute from a class method in Java.
Could you quickly convince me about which one of the 3 solutions below (or a totally different one :P) is a good practice?
public class Test {
String a;
public String getA(){
return this.a;
}
public setA(String a){
this.a = a;
}
// Using Getter
public void display(){
// Solution 1
System.out.println(this.a);
// Solution 2
System.out.println(getA());
// Solution 3
System.out.println(this.getA());
}
// Using Setter
public void myMethod(String b, String c){
// Solution 1
this.a = b + c;
// Solution 2
setA(b + c);
// Solution 3
this.setA(b + c);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这完全取决于 getter 和 setter 正在做什么。如果它们所做的不仅仅是获取和设置值(应该在方法的 Javadoc 中明确记录),那么您从类内部选择的方式确实会产生影响。但如果它们是像 getters/setters 这样的纯 Javabean,那么我宁愿通过
a
或this.a
直接访问变量,具体取决于变量中是否有局部变量范围与该名称完全相同。就我个人而言,只要我想做的事情不仅仅是获取/设置值,我就会根据 Javabean 规范保持 getter 和 setter 的“纯粹”,并添加另一个具有不言自明的方法名称的 getter 或 setter。例如
getAndIncrement()
、getAsString()
、setAsInt(String)
等。随喜好而定。只要您在整个编码过程中保持一致,它就不会真正造成损害。
That entirely depends on what the getters and setters are doing. If they do more than just getting and setting the value (which should be explicitly documented in the method's Javadoc), then it would really make difference what way you'd choose from inside the class. But if they are pure Javabean like getters/setters, then I'd rather access the variable directly by either
a
orthis.a
depending on whether there's a local variable in the scope with exactly that name.Personally I would just keep the getters and setters "pure" according the Javabean spec and add another getter or setter with a self-explaining method name whenever I'd like to do something more than just getting/setting the value. E.g.
getAndIncrement()
,getAsString()
,setAsInt(String)
, etc.Matter of taste. It won't really harm as long as you're consistent with it throughout your coding.
如果您将来需要创建任何验证,您将需要一个 setter/getter 。当您使变量可见时,您就破坏了类封装。理论上,这意味着您的代码不是面向对象的 :P ,但实际上您失去了进行大量代码重构的能力。例如,提取一个接口。
对于这个电话,我认为它是多余的,但这只是我:)
If you need to create any validation in the future you'll want a setter/getter . When you make a variable visible you brake the class encapsulation. In theory it means that your code is not so Object Oriented :P , but in practice you lose the ability to do a lot of code refactorings. For example, extracting a interface.
And for the this call, I think its redundant, but that's just me :)
如果涉及更多逻辑而不仅仅是返回 this.value,我会在类中使用 getter 和 setter。这样我就可以避免重复。举个例子:
我总是使用“this”,因为它让其他人更容易阅读我的代码。毫无疑问,this.value是一个类属性,而value既可以是局部变量,也可以是类属性。
I use getters and setters in a class if there is more logic involved that just returning this.value. This way I avoid duplication. An example:
I always use "this" because it makes it easier for other people to read my code. There is no doubt that this.value is a class attribute, whereas value can be both a local variable and a class attribute.
解决方案 2 或 3 是最佳实践,因为它们为现场提供了封装。例如,如果字段
'a'
是用户的邮政编码,并且您的应用程序有一个新要求,即始终以大写形式返回邮政编码,该怎么办?使用解决方案 2 或 3,这变得微不足道。例如,
您将只在一处进行更改,而不是在访问该字段的任何地方进行更改。添加
this
完全取决于您自己的个人风格或项目标准。就我个人而言,我不喜欢它,因为它是不必要的,而且只会妨碍可读性,但对于其他人来说,它使方法/字段的所有者更清晰。Solution 2 or 3 are best practice as they provide encapsulation to the field. For example, what if the field
'a'
is a user's postcode and your application has a new requirement to always return the postcode as uppercase. With solutions 2 or 3 this becomes trivial. E.g.becomes
and you will only have made the change in one place instead of anywhere where the field is accessed. The addition of
this
is purely up to your own personal style or project standards. Personally, I don't like it as it is unnecessary and just gets in the way of readability, but for others it makes the owner of method/field clearer.使用
setA(b + c)
是愚蠢的。获取器和设置器是接口的一部分。方法已经可以完全访问状态。坦白说吧。
如果您担心可能会破坏不变量,那么您的类对您来说太复杂了。(承认并重构。)
Using
setA(b + c)
is silly.Getters and setters are part of the interface. Methods already have full access to the state. Be frank about it.
If you're worried that you might break an invariant then your class is too complex for you. (Admit it and refactor.)
使用 getter 和 setter 是正确的方法。
,因此其他程序员更有可能理解您的代码。
,假设您想阻止某人将 a 设置为 null。暴露会员,你就永远做不到。
至于是否使用
this
- 我尝试一致地使用this
以使其他任何人都非常清楚哪些是实例成员,哪些是局部变量在任何时候 - 也有帮助避免意外的阴影,但我认为这不那么重要,更多的是一种风格。另外 -
this.a
是一个实例成员(每个实例一个)而不是类成员(每个类一个,将是静态的)。使用this
的另一个原因要明确。Using getters and setters is the way to go.
So other programmers are more likely to understand your code.
Say you want to prevent someone setting a to null. Expose the member and you can never do it.
As for whether to use
this
- I try to usethis
consistently to make it very clear to anyone else which are instance members and which are local variables at any point - also helps avoid accidental shadowing, but I think this is less important and more a style thing.Also -
this.a
is an instance member (one per instance) not a class member (one-per-class, would be static). Another reason to usethis
to be clear.我会同意
,
原因很简单,如果您想总体更改属性的访问方式或对可以设置变量的内容强制执行任何约束,则只需更改 getA 或 setA 方法即可。
我不喜欢使用它,除非我需要明确区分同名的变量。
I would go with
and
for the simple reason that if you wanted to generally change the way an attribute is accessed or enforce any constraints as to what you could set a variable to, you can just change the getA or setA methods.
I don't like using this unless I need to explicitly distinguish between variables of the same name.