简体   繁体   English

访问器和变量方法的混淆

[英]Accessor and Mutator Methods confusion

OK, basically the question was 好,基本上问题是

Accessor and Mutator Methods Suppose that the class Pet has a variable/field called name that is of type String> Accessor和Mutator方法假设Pet类具有一个名为name的变量/字段,其类型为String>

Write an accessor method getName() that returns the value of name Enter your answer in this box 编写一个访问器方法getName()返回name的值在此框中输入答案

and I wrote the code as following: 我写的代码如下:

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. 编写一个变幅方法setName(),其返回类型为void,当宠物更改所有者时,可用于更改name的值。 Enter your answer in this box 在此框中输入答案

and i wrote as following: 我写如下:

public void setName(int newName)
{
    name = newName;
}

I seem to be confused as I didnt get the mark, the error I got was 我没有获得分数似乎很困惑,我得到的错误是

 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

any ideas what wrong with it? 任何想法有什么问题吗?

Your method signatures are both incorrect: 您的方法签名都不正确:

  • name is String as you mentioned, but your mutator method takes an int which means integer as its input. 正如您所提到的,name是String ,但是您的mutator方法采用一个int ,即整数作为其输入。 Switch it to String and it should work. 将其切换为字符串,它应该可以工作。
  • Your accessor doesn't actually return the value of the internal name but instead the name parameter given to it. 您的访问器实际上并不返回内部名称的值,而是返回给它的名称参数。 This is an error because accessors are meant to return ( a copy of ) the value held internally by the class. 这是一个错误,因为访问器应返回类的内部保存的值( 的副本 )。 It's also missing the return type which should of course match the return type of the field. 它还缺少返回类型,该类型当然应该与字段的返回类型匹配。

Oh and just to clarify, method signature means the whole declaration of the method, basically everything you write to declare a method: 哦,为了澄清一下, 方法签名是方法的整个声明,基本上是您为声明方法而编写的所有内容:

  • Name is the obvious one. 名字是显而易见的。
  • Return value is part of method signature, really important since it's mandatory. 返回值是方法签名的一部分,由于它是强制性的,因此非常重要。
  • Visibility ( public/package/protected/private ) is important too because it defines who can access the actual method. 可见性( public / package / protected / private )也很重要,因为它定义了谁可以访问实际方法。
  • Parameters are of course part of the method signature because even if other parts of the method would be entirely same all the way to the name, parameters can make the method entirely unique. 参数当然是方法签名的一部分,因为即使方法的其他部分与名称完全相同,参数也可以使方法完全唯一。 Even zero parameters is counted as part of the signature! 甚至零个参数也算作签名的一部分!

The reason it's called signature is of course that these four together form a unique combination which act as an unique identifier to the method and you need all four to have a fully declared method with unique signature. 之所以称其为签名,是因为这四个一起形成一个唯一的组合,该组合充当该方法的唯一标识符,并且您需要所有四个都具有一个具有唯一签名的完全声明的方法。 The whole idea of accessors/mutators is relying on a certain pattern for method signatures which enables everyone to access them in a predefines, consice manner. 访问器/更改器的整个思想依赖于方法签名的某种模式,该模式使每个人都可以以预先定义的便捷方式访问它们。

NoClassDefFoundError usually means something is wrong with classpath, you need to actually add your class to the classpath to be able to run it. NoClassDefFoundError通常意味着类路径出了点问题,您实际上需要将您的类添加到类路径中才能运行它。

仔细阅读错误消息:“返回类型为必需的公共getName(String name)”。

Your accessor should be: public String getName() { return name; 您的访问器应为:public String getName(){返回名称; } }

Your mutator should be: public void setName(String newName) { name = newName; 您的更改器应为:public void setName(String newName){name = newName; } }

and you data should be protected like this 并且您的数据应该像这样被保护

private String name; 私有字符串名称;

我的建议是考虑所有注释,在JAVA中使用某些IDE进行开发,例如Eclipse ,IDE将最终改善您的开发时间,并避免坑洼。

Read the error message: return type required public getName(String name) It means that you need a return type for the method. 读取错误消息: return type required public getName(String name)这意味着您需要该方法的返回类型。 You need to add String after the void so you will have: 您需要在void之后添加String ,这样您将拥有:

public String getName(String name)
{
    return name;
}

You also need to change the int in your setName method to String . 您还需要将setName方法中的int更改为String

And you need to make sure you are creating the variable name at the beginning of the code: 并且您需要确保在代码的开头创建变量名:

private String name;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM