我目前在一个项目中工作,我有两个不同的 Enums
,它们应该填充到一个 Array
中。 IE。我的数组应该能够容纳其中一个枚举。我知道您不能像使用类那样为枚举实现继承。
有没有一种方法可以将我的两个枚举分组,以便将它们放入同一个数组中?
最佳答案
安enum
在 Java 中是 Enum
的隐式子类类(class)。与大多数object-oriented一样语言,Java 不支持 multiple inheritance .所以你的枚举不能是任何其他类的子类。
共享界面
但是……枚举可以实现接口(interface)。因此,定义一个由您的两个枚举实现的接口(interface)。
例如,让我们为各种animal breeds定义一个接口(interface)Breed
.
package work.basil.demo.multienum;
public interface Breed
{
}
定义两个枚举中的每一个,都实现该接口(interface)。
一些狗。
package work.basil.demo.multienum;
public enum DogBreed implements Breed
{
AUSTRALIAN_SHEPHERD , LABRADOR_RETRIEVER , BORDER_COLLIE
}
还有猫。
package work.basil.demo.multienum;
public enum CatBreed implements Breed
{
MAINE_COON , MANX , RUSSIAN_BLUE
}
在示例应用中试用它们。
package work.basil.demo.multienum;
import java.util.List;
public class App
{
public static void main ( String[] args )
{
List <Breed> breeds =
List.of(
DogBreed.BORDER_COLLIE,
CatBreed.RUSSIAN_BLUE,
CatBreed.MAINE_COON
);
System.out.println( "breeds = " + breeds );
}
}
运行时。
breeds = [BORDER_COLLIE, RUSSIAN_BLUE, MAINE_COON]
哎呀,我现在看到你要求一个数组。工作方式相同。
package work.basil.demo.multienum;
import java.util.Arrays;
import java.util.List;
public class App
{
public static void main ( String[] args )
{
Breed[] breeds =
List.of(
DogBreed.BORDER_COLLIE ,
CatBreed.RUSSIAN_BLUE ,
CatBreed.MAINE_COON
)
.toArray( new Breed[ 0 ] );
System.out.println( "breeds = " + Arrays.toString( breeds ) );
}
}
在这个例子中,我们没有在接口(interface) Breed
上定义任何方法。请注意,如果您在此处添加任何方法,则两个枚举都必须实现这些方法。如果省略,编译器会通知您。
类型和转换
您在评论中提问:
So, my Interface would work as a "super type" without defining anything to override. Do I understand this correctly?
是的。请注意我上一个示例中的数组声明。名为 breeds
的变量是 Breed
类型的对象数组(接口(interface))——不是 CatBreed
枚举对象数组,也不是DogBreed
枚举对象。
您在评论中提问:
What happens if an enum implements a method on its own? e.g.
CatBreed
implementspurr()
method. Can I still access this method even though the type isBreed
?
没有。数组的元素都是 Breed
对象。因此,当用作 Breed
时,这样的对象不会“看到”任何特定于 CatBreed
的方法。 Breed
对象只能看到在 Breed
接口(interface)上定义的方法。
要访问你的猫的 purr
方法,你必须转换 reference Breed
对象是对 CatBreed
对象的引用。您将首先使用 instanceof
进行测试。
Breed breed = breeds.get( 1 ) // Index of 1 means second item, per annoying zero-based counting. We get `CatBreed.RUSSIAN_BLUE` as seen in the example code above.
if( breed instanceOf CatBreed ) // Test if the object referenced by `breed` happens to be also of type `CatBreed` besides being of type `Breed`.
{
CatBreed catBreed = (CatBreed) breed ; // Cast to that other type.
… // Use the `CatBreed` object named `catBreed` to access methods specific to `CatBreed` enum class.
}
Java 16 有一个新特性,JEP 394: Pattern Matching for instanceof , 使类型转换自动化。
Breed breed = breeds.get( 1 ) // We get `CatBreed.RUSSIAN_BLUE` from earlier example.
if( breed instanceOf CatBreed catBreed ) // Casting is automatic in Java 16 and later.
{
… // Use the `CatBreed` object named `catBreed` to access methods specific to `CatBreed` enum class.
}
由于模式匹配的进一步工作, future 可能会出现其他相关的漂亮功能,records , 和 sealed classes .
警告:此设计(枚举共享一个已实现的接口(interface))可能是也可能不是特定设计问题的适当解决方案。为了展示这种解决方案的方法,我在给出的示例中忽略了适当性问题。
关于java - 如何将枚举与父类(super class)型分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67145823/