Java中的枚举enum

发布于 2024-12-11 06:23:02 字数 335 浏览 1 评论 0原文

我无法理解一个问题。问题是: 编写一条语句,使Terms枚举中的常量无需限定即可可供类使用

这是我到目前为止的代码;

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'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;
     }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮云壑 2024-12-18 06:23:02

我认为他们指的是 静态导入

示例:

import static mypackage.Term.*;

这将使您能够使用如下代码:

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

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 ...
}
凉月流沐 2024-12-18 06:23:02

您可能正在寻找

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);
     }
}

这里有一点需要注意。如果您的Terms.java位于默认包中,则无法对其进行静态导入。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文