简体   繁体   English

Java中的枚举枚举

[英]Enumeration enum in Java

I'm having trouble understanding a question. 我无法理解一个问题。 The question is: Code a statement that will make the constants in the Terms enumeration available to a class without qualification 问题是:编写一个语句,该语句将使类枚举中的常量可供无条件的类使用

This is my code so far; 到目前为止,这是我的代码;

public enum Terms {

    NET_30_DAYS, NET_60_DAYS, NET_90_DAYS; 
    //either name() or super() can be used

     @Override
     public String toString() {
         String s = "Net due " + name() + " days"; 
         return s;
     }
}

I think that they refer to static import . 我认为它们是指静态导入

Example: 例:

import static mypackage.Term.*;

This will enable you to use your code like: 这将使您可以像以下方式使用代码:

public void doSomething(Term term)
{
    if (NET_30_DAYS.equals(term))
    {
        ...
    }
    else if ...
}

You're probably looking for 您可能正在寻找

import static package.Terms.*;

public class Foo {

     public void aMethod() {
             System.out.println(NET_30_DAYS);
             // you can do the above instead of System.out.println(Terms.NET_30_DAYS);
     }
}

There is one thing to note here. 这里有一件事要注意。 If your Terms.java is in a default package, there is not a way to do a static import on it. 如果您的Terms.java位于默认程序包中,则无法对其进行静态导入。

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

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