[英]Java : accessor method? mutator method?
Consider a potential instance method named rotate that would rotate a Rectangle object 90 degrees by swapping its width and height values. 考虑一个名为rotate的潜在实例方法,该方法将通过交换Rectangle对象的宽度和高度值将其旋转90度。 For example, if a Rectangle's dimensions are 10 x 30, then calling the rotate method would change its dimensions to 30 x 10.
例如,如果Rectangle的尺寸为10 x 30,则调用rotate方法会将其尺寸更改为30 x 10。
I think this will be an accessor method, right? 我认为这将是一种访问器方法,对吗? Since we are not actually changing the values?
既然我们实际上没有更改值?
Accessor Methods 存取器方法
An accessor method is used to return the value of a private field.
访问器方法用于返回私有字段的值。
Mutator Methods 变异器方法
A mutator method is used to set a value of a private field.
mutator方法用于设置私有字段的值。
So here as you are changing the state of private fields so it is a mutator. 因此,在这里,当您更改私有字段的状态时,它就是一个变量。
Any method that changes an object's state is properly a mutator. 任何更改对象状态的方法都是正确的变种器。 As you are changing the dimensions (through the swap) it is a mutation.
在更改尺寸(通过交换)时,这是一个突变。 These would be accessor methods given your class definition,
给定您的类定义,这些将是访问器方法,
public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.