Getter-Setter和私有变量

如果我可以通过getter-return引用更改私有变量的值,那么它是否绕过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; } 

是的,它确实。 我在“ 清洁准则”一书中的获取者和制定者中得出以下结论; 如果你真的接受它,你可以使用它。

  1. 非常邪恶:公共领域。
  2. 有点邪恶:吸气剂和二传手不需要他们。
  3. 好的:只有在真正需要的地方才能获得getter和setter – 使类型暴露出“更大”的行为,恰好使用其状态,而不仅仅是将类型视为要由其他类型操纵的状态存储库。

实际上,正确的方法是仅为维度的所需部分提供一个setter。 喜欢这个:

 public int getDimensionX() { return cannotBeChanged.getX(); } public int getDimensionY() { return cannotBeChanged.getY(); } 

程序员应该设计外部实体触及其程序的安全变量的方法。

  1. 永远不要为对象的安全属性创建任何setter。 只能提供吸气剂。
  2. 仅为那些可在程序过程中更改的属性创建setter。
  3. 如果您想对某些属性应用某些限制,例如应用无效值检查,预填充,逻辑分析,填充另一个依赖属性,防御性复制等,请使用setter
  4. Getters / setters有助于维护系统的软件熵。 阅读有关软件熵的信息
  5. 不要创建getter / setter,因为它不需要它作为Boilerplate代码的引导。
  6. Getters / setter有助于更改未来程序扩展的底层实现,例如升级日志库等

这里有一个简单的Testcase来自我。 如您所见,您可以更改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; } } 

我的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); } } 

输出:

 Test|Testy 3|5 

私有变量只能从声明的类中访问。 当您创建返回私有变量值的getter方法时,您没有获取地址,而是创建一个保存返回值的临时副本。 setter方法为私有变量设置一个值,当来自另一个类时,该变量无法完成。

所以基本上getter-setter方法适用于您尝试从另一个类访问或修改私有变量的时候。

注意:您要修改的宽度和高度值是Dimension类中的变量,因此它们是公共的而不是私有的。

看看这个例子:

 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()); } } 

返回:

 3.0 5.0