在单独的包中使用嵌套枚举作为参数

发布于 2024-10-09 00:35:23 字数 383 浏览 0 评论 0原文

java 新手,有一个与包相关的问题。

我喜欢将对象组织在名称空间中,但遇到了我无法弄清楚的枚举问题。

假设我有一个像这样的嵌套枚举:

package Project;
public class Foo
{
    public enum Bar { One, Two, Three };
}

我想做这样的事情

package Project.Attributes;
public class Foo
{
    public setBar( Project.Foo.Bar bar ) {}
}

但是我遇到了名称冲突和未知包“Foo”错误。

我怎样才能实现这个目标?

New to java and have a question related to packages.

I like to keep objects organized in namespaces and ran into a problem with enums that I cannot figure out.

Say I have a nested enum like this:

package Project;
public class Foo
{
    public enum Bar { One, Two, Three };
}

I want to do something like this

package Project.Attributes;
public class Foo
{
    public setBar( Project.Foo.Bar bar ) {}
}

But I am getting name conflicts and unknown package 'Foo' errors.

How can I achieve this?

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

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

发布评论

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

评论(2

夜司空 2024-10-16 00:35:23

以下更正分号后工作,

  1. 它应该在包声明后的
  2. 为 setBar(...) 方法添加 void 返回类型

还有一个建议是对包名称使用小写。

package project;

public class Foo
{
    public enum Bar {
        One, Two, Three
    };
}


package project.attributes;

public class Foo
{
    public void setBar(project.Foo.Bar bar)
    {
    }
}

It should work after below corrections

  1. semicolon after the package declaration
  2. add void return type for setBar(...) method

One more suggestion is to use small case for package names.

package project;

public class Foo
{
    public enum Bar {
        One, Two, Three
    };
}


package project.attributes;

public class Foo
{
    public void setBar(project.Foo.Bar bar)
    {
    }
}
浅忆流年 2024-10-16 00:35:23

第一行末尾需要有一个分号。并使用小写字母作为包名称

You need to have a semicolon at the end of the first line. And use lowercase letters for package names

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