简体   繁体   English

用字段Java创建一个类

[英]Creating a class with fields java

I'm trying to create a class that represents a person's name and that has fields representing the first name, middle initial, and last name, (also several methods). 我正在尝试创建一个代表一个人的名字的类,并且该类具有代表名字,中间名首字母和姓氏的字段(还有几种方法)。

When I try to run my code, I receive a Illegal modifier for the local class Name; only abstract or final is permitted 当我尝试运行代码时,我收到一个Illegal modifier for the local class Name; only abstract or final is permittedIllegal modifier for the local class Name; only abstract or final is permitted Illegal modifier for the local class Name; only abstract or final is permitted error and cannot find a way to resolve this. Illegal modifier for the local class Name; only abstract or final is permitted错误,并且找不到解决此问题的方法。 Please help me resolve any errors so that my code will perform the desired (and commented) tasks. 请帮助我解决所有错误,以便我的代码执行所需的(和已注释的)任务。 Thank you in advance. 先感谢您。

public class Name {

    String first_name;
    String middle_initial;
    String last_name;

    //constructor that accepts first name, middle initial, and last name as parameters and initializes the Name object's state with those values
    public Name(String first_name, String middle_initial, String last_name) {
        first_name = "John";
        middle_initial = "Q.";
        last_name = "Public";
    }

    //method to return the person's name in first name, middle initial, and last name order.
    public String getNormalOrder() {
        String name = first_name + " " + middle_initial + " " + last_name;
        return name;    
    }

    //method to return the person's name in reversed order: last name, first name, and middle name.
    public String getReverseOrder() {
        String name = last_name + ", " + first_name + " " + middle_initial;
        return name;
    }

    //method to return the full name in first, middle, last order, in String form
    public String toString() {
        return first_name + " " + middle_initial + " " + last_name;
    }

    public static void main(String[] arg){
        first_name = "John";
        middle_initial = "Q.";
        last_name = "Public";

        getNormalOrder();
        getReverseOrder();
        toString();
    }

}

I suggest you start over with a file Name.java . 我建议您从文件Name.java

From the top, you can only have one public class of the same name as the file it is contained within. 从顶部开始,只能有一个public class ,其名称与其包含的文件相同。

public class Name {

}

Now, your field definitions are fine. 现在,您的字段定义很好。

The constructor, however, should not hard-code the values of "John Q. Public", as this Name class can represent any instance of Name . 但是,构造函数不应硬编码“ John Q. Public”的值,因为此Name类可以表示Name 任何实例。

Therefore, you need to assign the fields using this , which is needed to "qualify" which variable you are using since they are the same variable names. 因此,您需要使用this分配字段,由于它们是相同的变量名称,因此需要“限定”使用的变量。 (If you renamed String first_name here, to String fName , for example, then first_name = fName would be possible). (例如,如果您在此处将String first_name重命名为String fName ,则first_name = fName是可能的)。

public Name(String first_name, String middle_initial, String last_name) {
    this.first_name = first_name;
    this.middle_initial = middle_initial;
    this.last_name = last_name;
}

Then, in the main method, you want to make an instance of this object. 然后,在main方法中,您想要创建该对象的实例。

Name name = new Name("John", "Q.", "Public");

That's it. 而已。

But if you want to display those values, simply calling your methods isn't correct, as it needs to know which Name value to call the methods on. 但是,如果你想显示这些值,只需调用你的方法是不正确的,因为它需要知道哪些 Name值来调用方法。 You also need to print the String values that are returned to even see them. 您还需要打印返回的String值以查看它们。

System.out.println(name.getNormalOrder());
System.out.println(name); // calls name.toString()

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

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