标识符预期错误(Java)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15643620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:23:14  来源:igfitidea点击:

Identifier expected error (Java)

javaclasscompiler-errorsidentifier

提问by user2208377

I am new to programming and could use a helping hand. I've created a "Person" class and a TestPerson file to test it (yes, this is an assignment.)

我是编程新手,可以伸出援助之手。我创建了一个“Person”类和一个 TestPerson 文件来测试它(是的,这是一个作业。)

I am getting three errors "identifier expected" on lines 34-36 (in the setters). Clearly, this is a rookie error, but I just can't seem to figure it out. Any help would be appreciated. Thanks, in advance.

我在第 34-36 行(在 setter 中)收到三个“预期标识符”错误。显然,这是一个新手错误,但我似乎无法弄清楚。任何帮助,将不胜感激。提前致谢。

/******************************************************************
*     program name:      Person.java
*     author:           Nichol Albertson
*     remarks:          describes a Person object
      Date:             3/26/13
********************************************************************/
import java.text.*;

class Person {
    /**********************************************
     *  Local Variables
     ******************************************/
    private String lastName;
    private String firstName;
    private String birthday;

    /**********************************
        *   constructors - just a method
        ***********************************/

    public Person(String lastName, String firstName) {
        setLastName();
        setFirstName();
        setBirthday();
    }

    public Person(String lastName, String firstName, String birthday) {
        initialize(lastName, firstName, birthday);
    }

    /**********************************
    *   set methods  (setters)
    ***********************************/
    public void setLastName(lastName);
    public void setFirstName(firstName);
    public void setBirthday(birthday);

    /**********************************
    *   get methods  (getters)
    ***********************************/
    public String getLastName() {
        return lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getBirthday() {
        return birthday;
    }

    /*******************************
    * other methods
    ********************************/
    public String getFirstLast() {
        System.out.println(firstName + " " + lastName);
    }

    public String getLastFirst() {
        System.out.println(lastName + ", " + firstName);
    }
}

回答by rgettman

You will need to provide a type for the parameter in each of your setter methods. The type should match the type of the instance variable you're attempting to set.

您需要为每个 setter 方法中的参数提供类型。该类型应与您尝试设置的实例变量的类型相匹配。

Additionally, you should provide a method body with braces {and }for your setter methods, where you assign the parameter value to your instance variable.

另外,你应该提供一种方法,用身体支撑{}您setter方法,在这里您将参数值分配给您的实例变量。

回答by Vishal K

Error is showing because of following lines:

由于以下几行而显示错误:

public void setLastName(lastName);
public void setFirstName(firstName);
public void setBirthday(birthday);

Personis neither abstract classnor an interface. It is a concrete classindeed.You can't just declare method within the concreteclass . You have to provide body of methods also. This could be changed in this way:

Person既不是abstract class也不是interface。它确实是一个。concrete class你不能只在concrete类中声明方法。您还必须提供方法体。这可以通过这种方式改变:

public void setLastName(String lastName){ this.lastName = lastName;}
public void setFirstName(String firstName){ this.firstName = firstName;}
public void setBirthday(String birthday){ this.birthday = birthday;}

Moreover, following methods are defined completely wrong:

此外,以下方法的定义完全错误:

public String getFirstLast() {
    System.out.println(firstName + " " + lastName);
}

public String getLastFirst() {
    System.out.println(lastName + ", " + firstName);
}

Both of above methods have return type Stringbut you are returning nothing . This is also an error in your code. They should be defined as follows:

上述两种方法都有返回类型,String但你什么都不返回。这也是您代码中的错误。它们应定义如下:

public String getFirstLast() {
    return firstName + " " + lastName;
}

public String getLastFirst() {
    return lastName + ", " + firstName;
}

回答by Code Enthusiastic

public void setLastName(lastName);
public void setFirstName(firstName);
public void setBirthday(birthday);

If the class is a normal class, it is neither interface nor abstract class, so a method must have the body and the other point is you need to mention the types of variables lastName, firstName and birthday.

如果类是普通类,既不是接口也不是抽象类,所以一个方法必须有主体,另外一点是你需要提到变量lastName、firstName和birthday的类型。

   public void setLastName(String lastName)
   {
       //method's body
   }