【问题标题】:error in the enum class [closed]枚举类中的错误[关闭]
【发布时间】:2016-11-28 17:30:31
【问题描述】:
public enum MaritalStatus
{
    Single, Divorcee, Married

}

public class Person
{
    protected int ID;
    protected String FirstName;
    protected String LastName;
    protected MaritalStatus Status;
    protected int Age;

    public Person(int id,String firstname,String lastname,MaritalStatus status,int age)
    {
        ID=id;
        FirstName=firstname;
        LastName=lastname;
        Status=status;
        Age=age;
    }
    public String toString()
    {
        return System.out.println("ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ StringStatus +"  Age: "+Age);
    }
} 

Person.java:19: error: incompatible types: MaritalStatus cannot be converted to String
        return System.out.println("ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ (String)Status +"    Age: "+Age);
                                                                                                                               ^
1 error

【问题讨论】:

  • 因此错误消息中的箭头正好指向问题所在。但是您没有发布此代码,而是发布了另一个没有问题的代码。
  • StringStatus 是从哪里来的?应该是status.toString()

标签: java class enums


【解决方案1】:

不,没有“枚举类中的错误”。您在toString 中遇到问题(见下文)。您可能需要以下内容

public String toString()   {

  return  "ID: "+ID + " First Name: "+FirstName+"   Last Name: " +LastName+"    Marital Status: "+ Status +"    Age: "+Age;
}

问题:

  • 您不能返回 System.out.println(…)(它返回 void,它不会“打印并返回字符串”)
  • 要获得status 的字符串化版本,只需在此上下文中使用status(您在字符串上使用+)或在其他上下文中使用status.toString()(需要String 类型)。

其他(不相关的)问题

  • java 中的字段/变量/参数通常以小写开头(idfirstName 等)
  • 字段通常是private 而不是protected
  • 大多数人更喜欢运算符周围的空格,例如赋值 (a = b)
  • (恕我直言)尽可能使用final(如其他语言中的valconst
  • 枚举字段通常为大写(SINGLEMARRIED

这是您的课程的(更)正确版本:

public class Person {
    private int id;
    private String firstName;
    private String lastName;
    private MaritalStatus status;
    private int age;

    public Person(final int id, final String firstName, final String lastName, final MaritalStatus status, final int age) {
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
        this.status = status;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", status=" + status +
                ", age=" + age +
                '}';
    }
}

附言我猜这不是生产代码,因为有一个名为 age 的字段有点尴尬

【讨论】:

    【解决方案2】:

    我可以看到两个编译错误,加上一大堆样式错误。

    这一行:

    return System.out.println("ID: "+ID + " First Name: "+FirstName+
       "    Last Name: " +LastName+"    Marital Status: "+ StringStatus +
       "    Age: "+Age);
    

    1) 变量StringStatus 尚未声明。

    1a) 然后您将其更改为 (String) Status,这也是不正确的,因为您无法将 MaritalStatus 转换为 String。 (但你可以打电话给toString() ....)

    2) println 方法是返回 void 而不是 String 的方法。

    主要的样式错误1是:

    • 字段名称不应以大写字母开头。
    • 您在标记之前/之后使用空格是非标准且不一致的。在+= 等中缀运算符前后放置一个空格。 , 之前没有空格,之后有一个空格。
    • 使用超过 150 个字符的行是非标准的......这会使您的代码难以阅读。最多 80 个字符行最有利于提高可读性。
    • 缩进 8 个空格过多。
    • 字段应为private 而不是protected

      这不仅仅是简单的风格。如果您没有将字段声明为private,那么子类或(更糟糕的)一些其他不相关的代码可能会干扰该字段的值。这意味着您必须阅读更多代码才能了解在跟踪错误时可能发生的情况(例如)。使用private 有助于封装类型,这使代码更易于阅读和推理。

    IMO,处理相同的参数名称和字段名称(例如在构造函数中)的最佳方法是这样的:

    public class Person {
        protected int id;
    
        public Person(int id) {
            this.id = id;   // Use 'this' to disambiguate the two meanings 
                            // of the 'id' identifier.
        }
    }
    

    1 - 不幸的是,一些“乐于助人”的人已经纠正了很多问题,以使您问题中的代码可读。请参考您最初发布的内容。

    【讨论】:

      猜你喜欢
      • 2015-02-24
      • 1970-01-01
      • 1970-01-01
      • 2016-04-16
      • 1970-01-01
      • 1970-01-01
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多