English 中文(简体)
Java enum 留下错误吗?
原标题:Java enum giving an error?
  • 时间:2012-01-12 13:17:32
  •  标签:
  • java
  • enums

当我试图给我以价值时,它给我这个错误:

不能将建筑状况适用于特定类型;

为什么发生这种情况,我如何确定这种情况?

我的法典是:

 public enum Status 
 { 
     STATUS_OPEN(0),  
     STATUS_STARTED(1),  
     STATUS_INPROGRESS(2),  
     STATUS_ONHOLD(3),  
     STATUS_COMPLETED(4),  
     STATUS_CLOSED(5);  

 }

我用笔记和“JDK”迅速使用,但此时我不想使用净豆或ec。

I was following this site: link

我ve了周围,我无法真正找到为什么出现这一问题,或如何通过寻找错误来加以纠正。

最佳回答

You need to add a constructor to the enum.

public enum Status {
   STATUS_OPEN(0),  
   STATUS_STARTED(1),  
   STATUS_INPROGRESS(2),  
   STATUS_ONHOLD(3),  
   STATUS_COMPLETED(4),
   STATUS_CLOSED(5);

   private final int number;
   Status(int number) { 
       this.number = number;
   }

   public int getMagicNumber() { return number; } 
}

This ll fix your syntax problems, but what are you hoping to achieve with the number? Enums are often used instead of the need for numbers at all.

问题回答

页: 1 www.un.org/Depts/DGACM/index_french.htm ......

public enum Status 
 { 
        STATUS_OPEN(0),  
        STATUS_STARTED(1),  
        STATUS_INPROGRESS(2),  
        STATUS_ONHOLD(3),  
        STATUS_COMPLETED(4),  
        STATUS_CLOSED(5); 

       private int status;

       private Status(int status){
        this.status = status;
       }

     public int getStatus(){
       return this.status;
      } 
 }




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签