简体   繁体   English

关于 Java 枚举的问题

[英]Question about Java enums

I have 2 files which are interacting with each other.我有 2 个相互交互的文件。 I wanted to define an enum to make the code more readable, but if I define it in file 1, file 2 complains about having no knowledge of said enum.我想定义一个枚举以使代码更具可读性,但如果我在文件 1 中定义它,文件 2 会抱怨不知道所述枚举。 If I define ii in file 2, file 1 does the same.如果我在文件 2 中定义 ii,文件 1 也会这样做。 I am defining it as public too.我也将其定义为公开的。

The solution was to define the enum in both files, but this doesn't seem right to me.解决方案是在两个文件中定义枚举,但这对我来说似乎不正确。 Not only is it redundant, but I fear it may cause some conflict, even if the types have the same items.它不仅是多余的,而且我担心它可能会导致一些冲突,即使类型具有相同的项目。

What is the veredict on this?对此有何判决? Am I doing something wrong or worrying too much?我做错了什么或担心太多了吗?

EDIT编辑

Well, given the comments here I found an alternative which seems to be doing what I want without having to create a new file.好吧,鉴于这里的评论,我找到了一个替代方案,它似乎正在做我想做的事,而无需创建新文件。 I had:我有:

file 1文件 1

class myClass1
{
    public enum MyEnum
    {
        ...
    }
    ...
}

file 2文件 2

class myClass2
{
    public enum MyEnum
    {
        ...
    }
    ....
}

Now, I have:我现在有:

file 1文件 1

enum myEnum
{
    ...
}

...

class myClass1
{
     ...
}

file 2文件 2

class myClass2
{
     ...
}

I didn't want to create another file just for the enum, so this works for me.我不想仅为枚举创建另一个文件,所以这对我有用。 Well, as long as there is nothing wrong with it, which I think there isn't.好吧,只要它没有任何问题,我认为没有。

You definitely shouldn't define the enum in both locations.您绝对不应该在两个位置都定义枚举。 I recommend defining the enum in its own file with public accessibility.我建议在具有公共可访问性的自己的文件中定义枚举。 Then everyone should have no trouble accessing it.那么每个人都应该可以毫无问题地访问它。 However, assuming that you want to define the enum in one of the two classes, I'll continue...但是,假设您想在两个类之一中定义枚举,我将继续......

You have to import the enum or use its fully qualified name.您必须import枚举或使用其完全限定名称。 Assuming you are in the package com.stackoverflow , your classes should look like this, in the first file:假设您在 package com.stackoverflow中,您的类在第一个文件中应该如下所示:

package com.stackoverflow;

public class A {
    public enum MyEnum {
      ONE,TWO,THREE;
    }

    ...
}

and in another file:在另一个文件中:

package com.stackoverflow;

import com.stackoverflow.A.MyEnum;

public class B {

  public void test(MyEnum mine) {
    ...
  }

  ...
}

No - it is wrong.不——这是错误的。 The enum can be declared as a static inner class and referenced using the fully-qualified name enum可以声明为 static 内部 class 并使用完全限定名称引用

public class MyClass {
    public static enum MyEnum { BOOK, DVD }


    public void myMethod() {
        MyEnum e = MyEnum.DVD;
    }
}


public class B {
    public void someMethod() {
        MyClass.MyEnum enumVal = MyClass.MyEnum.BOOK;
    }
}

Also remember when u put enum in another class's file you risk the problem of unable to access the enum from another package due to default access privilege.还请记住,当您将枚举放入另一个类的文件时,由于默认访问权限,您可能会面临无法从另一个 package 访问枚举的问题。 ... so It is always wise to put the enum in a separate file with public access or as a public static member of a class that can be accessed with the class name. ...所以将枚举放在具有公共访问权限的单独文件中或作为可以使用 class 名称访问的 class 的公共 static 成员始终是明智的。

You define your enums the same way you define classes - either in their own file, or as static inner classes (they'd have to be static not to be bound to their enclosing class).您定义枚举的方式与定义类的方式相同 - 在它们自己的文件中,或者作为 static 内部类(它们必须是 static 才能不受其封闭类的约束)。

Defining your enum in 2 files is broken, as it's sort of defeating the purpose - you'll have 2 separate enums, and while you'll want to test equality on Enum.A you'll actually have Enum1.A.=Enum2.A.在 2 个文件中定义你的枚举被破坏了,因为它有点违背目的 - 你将有 2 个单独的枚举,虽然你想在 Enum.A 上测试相等性,但实际上你会有 Enum1.A.=Enum2。一个。 Very confusing.非常混乱。

THere's no real mystery to them, it's just a nicety around a way to define constants that's safer and more capable than public static final.对他们来说,这并不神秘,它只是一种定义常量的方法,它比公共的 static final 更安全、更有能力。

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

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