[英]Writing a mutator method
Revision of mutator method definition 修改mutator方法的定义
Write a mutator method setAge() that takes a single parameter of type int and sets the value of variable age Paste your answer in here: 编写一个变数方法setAge(),该方法采用一个int类型的单个参数并设置变量age的值。在此处粘贴您的答案:
public int setAge(int age)
{
return age;
}
Comments: 评论:
* Test 1 (0.0 out of 1)
The compilation was successful
The output should have been:
setAge() Correct
This is what was actually produced:
setAge() not Correct
confused on why i get this error, is it because i have (int age) after setAge that is why the error is comming up? 为什么我会收到此错误感到困惑,是因为在setAge之后我有(int age)这就是为什么错误会出现的原因?
Your mutator is not actually setting anything. mutator实际上没有设置任何内容。
I assume you already have a piece of code that you have to modify, search in that piece for a variable/field 'age'. 我假设您已经有一段代码需要修改,请在那段代码中搜索变量/字段“ age”。
Try with 试试看
public void setAge(int age)
{
this.age = age;
}
Your code does not "set the value of a variable age". 您的代码不会“设置可变年龄的值”。 Your method only returns the value that was passed to it.
您的方法仅返回传递给它的值。
Assuming you have a class variable called 'age', you can create the mutator class method as: 假设您有一个名为'age'的类变量,则可以按以下方式创建mutator类方法:
public class myTest{ public int age; //other code here..if any public void setAge(int age) { this.age = age; } //other code here.. if any }
Normally your setAge method should not return anything. 通常,您的setAge方法不应返回任何内容。 Mutator only modifies the value.
Mutator仅修改值。
To return value you should use getAge() method which is called 'Accessor'. 要返回值,您应该使用称为“访问器”的getAge()方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.