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

标签 java

好的,基本上问题是

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/

相关文章:

java - Unix 时间戳在 DateTime 中不断返回 Jan 17 1970

java - 是否可以在 Android 中使用 phoneGap 在 sdcard 中加载/创建数据库?

java - 为什么我的自定义拦截器不起作用?

java - 将 json 转换为 POJO 时维护父类引用

java - catch语句的顺序?

java - 字符引用 "&#x10"是无效的 XML 字符

java - 是否可以检测堆外内存泄漏的根源?

java - Elasticsearch Java Rest 客户端 : how to get the list of all indices

java - 在Java/Android中获取年、月、日和时间形式 "Tue, 12 Jan 2016 09:40:07 GMT"

java - 使用Java将HDFS文件拆分为多个本地文件