Java Getter-Setter 和私有变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22008180/
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
Getter-Setter and private variables
提问by genonymous
If I can change the value of private variable through getter-returned reference then isn't it bypassing the setter method? Doesn't it defeat the purpose of getter-setter and private variables
如果我可以通过 getter 返回的引用更改私有变量的值,那么它是否绕过了 setter 方法?这不是违背了 getter-setter 和私有变量的目的吗?
public class Test{
private Dimension cannotBeChanged;
public Test(int height, int width)
{
if(height!=3)
cannotBeChanged.height = height;
if(width!=3)
cannotBeChanged.width = width;
}
public Dimension getDimension()
{
return cannotBeChanged;
}
public void setDimension(int height, int width)
{
if(height!=3)
cannotBeChanged.height = height;
if(width!=3)
cannotBeChanged.width = width;
}
public static void main(String [] args)
{
Test testOne = new Test(5,5);
Dimension testSecond = testOne.getDimension();
testSecond.height = 3; //Changed height and width to unwanted values
testSecond.width= 3;
}
采纳答案by Abimaran Kugathasan
Yes, It does. I have the following conclusion in getters and setters from the Clean Codebook; you can use it if you really accept it.
是的,它确实。我在Clean Code书中的getter 和 setter 中得出以下结论;如果你真的接受它,你可以使用它。
- Very evil: public fields.
- Somewhat evil: Getters and setters where they're not required.
- Good: Getters and setters only where they're really required- make the type expose "larger" behaviour which happens to use its state, rather than just treating the type as a repository of state to be manipulated by other types.
- 非常邪恶:公共领域。
- 有点邪恶:不需要的吸气剂和二传手。
- 好:Getter 和 setter 只在真正需要的地方- 使类型暴露“更大”的行为,它碰巧使用其状态,而不是仅仅将类型视为由其他类型操作的状态存储库。
回答by InBravo
Programmer should devise the ways for external entities to touch the secured variables of his program.
程序员应该设计外部实体接触其程序的安全变量的方法。
- Never create any setter for a secured property of your object. Only a getter can be provided.
- Create setters only for those properties, which can change during the course of program.
- Use setters if you want to apply certain restrictions on some properties e.g. apply invalid value checks, pre-population, logical analysis, populating another depending property, defensive copying etc
- Getters/setters helps in maintaining the software entropy of a system. Read about software entropy.
- Do not create getters/setters where it is not required as its leads to Boilerplatecode.
- Getters/setters helps in changing the underlying implementation for future Extensions of Programs e.g. Upgrading Logging libraries etc
- 永远不要为对象的安全属性创建任何 setter。只能提供一个吸气剂。
- 仅为那些属性创建 setter,这些属性可以在程序过程中更改。
- 如果您想对某些属性应用某些限制,请使用 setter,例如应用无效值检查、预填充、逻辑分析、填充另一个依赖属性、防御性复制等
- Getters/setters 有助于维护系统的软件熵。阅读有关软件熵的信息。
- 不要在不需要的地方创建 getter/setter,因为它会导致Boilerplate代码。
- Getters/setters 有助于改变未来程序扩展的底层实现,例如升级日志库等
回答by Kenyakorn Ketsombut
The right way would actually be, to provide a setter only for the needed part of the dimension. Like this:
正确的方法实际上是,只为维度的所需部分提供一个 setter。像这样:
public int getDimensionX()
{
return cannotBeChanged.getX();
}
public int getDimensionY()
{
return cannotBeChanged.getY();
}
回答by Leo Pflug
Here a simple Testcase from me. As you can see you can change the height indeed of a Dimension, because it's a reference, but you can't set a new Dimension.
这是我的一个简单的测试用例。如您所见,您确实可以更改 Dimension 的高度,因为它是一个参考,但您无法设置新的 Dimension。
import java.awt.Dimension;
public class TestProperty
{
private String testy;
private Dimension testDim = new Dimension(2,2);
TestProperty(String testy)
{
this.testy = testy;
}
public String getTesty()
{
return testy;
}
public void setTesty(String testy)
{
this.testy = testy;
}
public Dimension getTestDim()
{
return testDim;
}
public void setTestDim(Dimension testDim)
{
this.testDim = testDim;
}
}
My main()-method:
我的 main() 方法:
import java.awt.Dimension;
public class Test
{
public static void main(String[] ARGS)
{
TestProperty testy = new TestProperty("Testy");
String myString = testy.getTesty();
Dimension myDimension = testy.getTestDim();
myDimension.height = 5; //Changes the height of the private Dimension
myDimension = new Dimension(5,3); //Does not set a new Instance of Dimension to my TestProperty.
myString = "Test";
System.out.println(myString+"|"+testy.getTesty());
System.out.println(myDimension.height+"|"+testy.getTestDim().height);
}
}
Output:
输出:
Test|Testy
3|5
回答by Belfer4
Private variables are meant to be accessed only from the class it was declared. When you create the getter method that returns the value of the private variable you are not getting the address but instead creating a temporary copy that holds the value of the returned value. The setter method sets a value to the private variable which can't be done when it's from another class.
私有变量只能从它声明的类访问。当您创建返回私有变量值的 getter 方法时,您不会获取地址,而是创建一个临时副本来保存返回值的值。setter 方法为私有变量设置一个值,当它来自另一个类时无法完成。
So basically the getter-setter methods are for when you are trying to access or modify private variables from another class.
所以基本上 getter-setter 方法用于当您尝试从另一个类访问或修改私有变量时。
Note: The width and height values you are modifying are the variables from the Dimension class so they are public not private.
注意:您正在修改的宽度和高度值是来自 Dimension 类的变量,因此它们是公共而非私有的。
Take a look at this example:
看看这个例子:
public class Test {
private double width, height;
public Test(int height, int width)
{
setDimension(height, width);
}
public double getWidth() { return width; }
public double getHeight() { return height; }
public void setDimension(int height, int width)
{
if(height!=3)
this.height = height;
if(width!=3)
this.width = width;
}
public static void main(String [] args)
{
Test test = new Test(5,5);
double testW = test.getWidth();
testW = 3;
System.out.println(testW);
System.out.println(test.getWidth());
}
}
Returns:
返回:
3.0
5.0