java - 使用带有枚举和标记接口的 switch 语句
问题描述
我有一个标记界面
public interface Marker{}
和两个实现标记的枚举
public enum Sharpie implements Marker{
RED,
BLUE,
BLACK
}
public enum Crayola implements Marker{
PURPLE,
ORANGE,
GREEN
}
我正在尝试做的是利用 switch 语句,例如
public boolean isOwned(Marker m){
// Take in a marker of either Sharpie, or Crayola
switch(m){
case BLUE:
case BLACK:
case GREEN:
return true;
default:
return false;
}
}
有没有办法在不使用昂贵的instanceof
电话的情况下做到这一点?
像这样的东西会起作用,但我试图避免使用instanceof
,坦率地说,它看起来有点难看。
public boolean isOwned(Marker m){
// First determine instanceof and then cast the marker
// to the appropriate Type before utilizing the switch statement
if (m instanceof Sharpie){
switch((Sharpie) m){
Case BLUE:
Case BLACK:
return true;
default:
return false;
}
} else {
switch((Crayola) m){
case Green:
return true;
default:
return false;
}
}
}
解决方案
看起来是为开关表达式尝试新的 Java 功能密封接口和模式匹配的好场景(* 这是 jdk 17 的预览功能)
首先做成Marker
密封接口
public sealed interface Marker permits Crayola, Sharpie {}
然后我们可以使用 switch 表达式来摆脱那些instanceof
检查。
public boolean isOwned(Marker marker) {
boolean isOwned = switch (marker) {
case Sharpie s -> s == Sharpie.BLACK || s == Sharpie.BLUE;
case Crayola c -> c == Crayola.GREEN;
};
return isOwned;
}
推荐阅读
- android - 在 Android Q 中从 SD 卡生成缩略图
- android - 什么是Impl文件,在什么条件下会自动生成Impl文件?
- python - KeyError:'无法找到名为 app 的无状态 DjangoApp'
- python - 无法在 anaconda 中安装 pal2nal 软件包
- sql - 如果 Oracle SQL 列中的数字是完整的整数,则添加两个带小数的零
- python - 使用 python 绘制 2D 步行
- javascript - 尝试将组件名称从字符串转换为 Angular 9 中的组件类型会导致传入的类型不是 ComponentType 错误
- c# - 想要使用 c# 在浏览器中连接到 Azure Windows 虚拟桌面?
- excel - csv 中的换行符导入 Excel
- asp.net-mvc - 是否有任何选项可以使用 ASP.NET Core 3.1 打开浏览器的“下载前询问每个文件的保存位置”?