gpt4 book ai didi

java - 访问器和修改器方法混淆

转载 作者:行者123 更新时间:2023-12-01 06:59:53 25 4
gpt4 key购买 nike

好的,基本上问题是

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com