好的,基本上问题是
Accessor and Mutator Methods Suppose that the class Pet has a variable/field called name that is of type String>
Write an accessor method getName() that returns the value of name Enter your answer in this box
我编写的代码如下:
public getName(String name)
{
return name;
}
Write a mutator method setName(), with a return type of void that may be used to change the value of name when the pet changes owners. Enter your answer in this box
我写如下:
public void setName(int newName)
{
name = newName;
}
我似乎很困惑,因为我没有得到分数,我得到的错误是
Pet.java:9: invalid method
declaration; return type required
public getName(String name)
^ 1 error The output should have been:
getName() works as expected
setName() works
This is what was actually produced:
Exception in thread "main" java.lang.NoClassDefFoundError: Pet
有什么问题吗?
最佳答案
仔细阅读错误消息:“return type required public getName(String name)”。
关于java - 访问器和修改器方法混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1639712/