可能是重复问题:
为什么要使用getter和setter?
我读过关于Java的书,其中提到为变量(如x
和y
)创建getter和setter方法是很好的做法。例如:
public int getX(){
return x;
}
public void setX(int x){
this.x = x;
}
但是这和那之间的区别是什么呢?
...(shape.x)... // Basically getX()
和
shape.x = 90; // Basically setX()
如果说setter和getter方法更好,那么会出现哪些实际问题?