java getter 和 setter 方法应该有什么名字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7213165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What names should getter and setter methods have
提问by Daniel Bezden
I am still very confused about getter and setter methods. I had this code;
我对 getter 和 setter 方法仍然很困惑。我有这个代码;
public class MethodsInstances {
public MethodsInstances(String name){
girlName = name;
}
private String girlName;
public String getName(){
return girlName;
}
public void sayName(){
System.out.printf("Your first gf was %s", getName());
}
}
But for "sayName", why couldnt you instead of using getName(), just type girlName? It would be the same, because getName() returns girlName, as seen in the code. Also, do the methods have to start with get and set, or can be named whatever you want?
但是对于“sayName”,为什么不能使用 getName() 而不是只键入 girlName?它会是一样的,因为 getName() 返回 girlName,如代码所示。此外,这些方法是否必须以 get 和 set 开头,或者可以随意命名?
Huge thanks from the newbie coder, Dan B
非常感谢新手编码员 Dan B
回答by Petar Ivanov
The point of getters and setters is that only they are meant to be used to access the private varialble, which they are getting or setting. This way you provide encapsulation and it will be much easier to refactor or modify your code later.
getter 和 setter 的要点是,只有它们用于访问它们正在获取或设置的私有变量。这样你就提供了封装,以后重构或修改代码会容易得多。
Imagine you use girlName
instead of its getter. Then if you want to add something like a default (say the default name is 'Guest' if it wasn't set before), then you'll have to modify both the getter and the sayName
function.
想象一下你使用girlName
而不是它的吸气剂。然后,如果您想添加类似默认值的内容(如果之前未设置,则默认名称为“Guest”),那么您必须同时修改 getter 和sayName
函数。
There is no requirement for getters and setter to start with get and set - they are just normal member functions. However it's a convention to do that. (especially if you use Java Beans)
getter 和 setter 不需要从 get 和 set 开始——它们只是普通的成员函数。然而,这样做是一种惯例。(特别是如果您使用Java Beans)
回答by rompetroll
You absolutely can use the variable directly in your example, mostly because sayName()
is in the same class.
您绝对可以在示例中直接使用该变量,主要是因为sayName()
它在同一个类中。
Other than that, I see 3 reasons for having getters and setters:
除此之外,我认为有 getter 和 setter 的 3 个原因:
1.) It's a principle of object oriented programming to keep values (state) private and provide public methods for interaction with other classes.
1.) 保持值(状态)私有并提供与其他类交互的公共方法是面向对象编程的原则。
2.) Classes with getters and setters often follow the Java beans design pattern. This pattern allows those objects to be used in template engines or expression languages like JSP or Spring.
2.) 带有 getter 和 setter 的类通常遵循 Java bean 设计模式。这种模式允许在模板引擎或表达式语言(如 JSP 或 Spring)中使用这些对象。
3.) In some cases it prevents actual errors. An example:
3.) 在某些情况下,它可以防止实际错误。一个例子:
public class DateHolder() {
public Date date;
public static void main(String... args) {
DateHolder holder = new DateHolder();
holder.date = new Date();
System.out.println("date in holder: "+holder.date);
Date outsideDateRef = holder.date;
outsideDateRef.setTime(1l);
//will be different, although we did not change anything in the holder object.
System.out.println("date in holder: "+holder.date);
}
}
wrapping the date variable with getter and setter that operate only with the value, not the reference, would prevent this:
使用仅使用值而不是引用操作的 getter 和 setter 包装日期变量将防止出现这种情况:
public class DateHolder() {
private Date date;
public Date getDate() {
return (Date)this.date.clone();
}
public void setDate(Date date) {
this.date = (Date) date.clone();
}
public static void main(String... args) {
DateHolder holder = new DateHolder();
holder.setDate( new Date() );
System.out.println("date in holder: "+holder.getDate());
Date outsideDateRef = holder.getDate();
outsideDateRef.setTime(1l);
//Date in holder will not have changed
System.out.println("date in holder: "+holder.getDate());
}
}
回答by fiction
You must first read about abstraction, encapsulationand OOPto understand about accessors, mutators, immutability and data access.
回答by Adnan Bhatti
You can use girlName here you really don't have to call getName(). The reason you need getName() is if you you want to get the name outside of this class. For example if you create a new class and then create the above class object in the new class and assign that object a name (value for girlName) you won't be able to access girlName from new class since it is private .. so you need a public method which will get the value for you.
你可以在这里使用 girlName 你真的不必调用 getName()。您需要 getName() 的原因是您想在该类之外获取名称。例如,如果您创建一个新类,然后在新类中创建上述类对象并为该对象分配一个名称(girlName 的值),您将无法从新类访问 girlName,因为它是私有的 .. 所以你需要一个公共方法来为您获取价值。
Also it doesn't have to be getName or setName but this just makes it easy to understand what function is doing.
此外,它不必是 getName 或 setName ,但这只是使理解函数在做什么变得容易。
回答by Silfverstrom
It's a common design patter to encapsulate the process of "getting" and "setting" variables in methods. This gives you more freedom if you ever want to change the underlying implementation.
在方法中封装“获取”和“设置”变量的过程是一种常见的设计模式。如果您想更改底层实现,这将为您提供更多自由。
For an example, lets say you one day want to change the parameter girlName to be an object Girl instead; If you directly access girlName from your outer classes, you will have to change all your external code.
例如,假设有一天您想将参数 girlName 改为对象 Girl;如果直接从外部类访问 girlName,则必须更改所有外部代码。
With a setter method, you could simply change one method and do
使用 setter 方法,您可以简单地更改一种方法并执行
public void setGirlname(String name)
{
girlname = new Girl(name, some_other_data);
}
Or perhaps you want to make sure girlname always is returned with uppercase.
或者您可能想确保 girlname 始终以大写形式返回。
public String getGirlname()
{
return girlName.toUpperCase();
}
Thus giving you a loot more flexibility in your code design.
从而为您的代码设计提供更大的灵活性。
回答by Reeta
We want to prevent direct access to the variable, we make the variable a private variable. When the variable is private, other classes are not able to access that variable. If we create variable as public it is accessible for all.
我们要防止直接访问变量,我们将变量设为私有变量。当变量是私有的时,其他类无法访问该变量。如果我们将变量创建为 public,则所有人都可以访问它。
to change the actual private variable we will now need public getter() or setter(). The basic naming conventions say that we will take the name of the variable and prefix it with get and/or set.
要更改实际的私有变量,我们现在需要 public getter() 或 setter()。基本的命名约定说我们将采用变量的名称并在它前面加上 get 和/或 set。
in your specific case the getGirlname would be correct.
在您的特定情况下, getGirlname 是正确的。
We call this encapsulation
我们称之为封装