Mutator method |
In computer science the mutator method is a method that changes the value of the argument variables.
The mutator method, sometimes called a setter , is often used in association with classes and object-oriented programming (OOP), in particular with the principle of encapsulation. In many modern languages that support object-oriented programming, such as C-plus-plus programming language and Java programming language, data variables belonging to a class are often private, or protected from outside interference, and can only be modified by a public function (the mutator function) belonging to the class that will receive in coming data, check it for error, and then modify the private member variable appropriately.
In the pass-by-value form of argument passing, a reference or Pointer to the data needs to be passed.
An example of a mutator method from the C++ language: class Person{ private: int age; //an integer data variable that stores the age for the person represented //...other variables as needed public: //...other methods as needed... //A mutator method that will check the parameter newAge, which is intended to //be the new value of age, as being greater than 0 and less than 150 before //storing it. void setAge(int newAge){ if (newAge > 0 && newAge < 150) age = newAge; } }|
|