In C#, I can do this.

[Flags]
enum BeerProperty
{
    Bold = 1,
    Refreshing = 2
}

static void Taste(BeerProperty beer)
{
    if (beer == (BeerProperty.Bold | BeerProperty.Refreshing))
    {
        Debug.WriteLine("I can't qutie put my finger on...");
    }
}

static void Main(string[] args)
{
    var tickBeer = BeerProperty.Bold | BeerProperty.Refreshing;
    Taste(tickBeer);
}

In Kotlin, it seems that I cannot "OR" two flags. What is the Kotlin's way to do this? Using a list of enum variables?

enum class BeerProperty(value:Int)
{
    Bold(1),
    Refreshing(2)
}

fun taste(beer:BeerProperty)
{
    if(beer == (BeerProperty.Bold | BeerProperty.Refreshing))
    {
        print("I can't qutie put my finger on...");
    }
}

fun main(args: Array<String>)
{
    val tickBeer = BeerProperty.Bold | BeerProperty.Refreshing;
    taste(tickBeer);
}

补充:谢谢你的回答(由于时间限制,我还不能将其标记为答案).我修改了下面的代码,实现了我想要的.

fun taste(beer: EnumSet<BeerProperty>)
{
    if(beer.contains(BeerProperty.Bold) && beer.contains(BeerProperty.Refreshing))
    {
        print("I can't qutie put my finger on...");
    }
}

fun main(args: Array<String>)
{
    val tickBeer = EnumSet.of(BeerProperty.Bold, BeerProperty.Refreshing);
    taste(tickBeer);
}

推荐答案

Indeed, in Kotlin every enum constant is an instance of the class corresponding to the enum, and you can't use 'OR' to combine multiple clas instances. If you need to track multiple enum values, the most efficient way to do that is to use the EnumSet class.

Kotlin相关问答推荐

Microronaut Data 4和JDbi

Kotlin扩展函数未调用Hibernate/JPA中的重写函数

限制通用Kotlin枚举为特定类型

为什么 Kotlin main 函数需要 @JVMStatic 注解?

使用 Jetpack Compose 使用参数导航

在kotlin中匹配多个变量

为什么 trySend 会发出假数据?

将 java Optional 转换为 Kotlin Arrow Option

如何从 var list 或可变列表中获取列表流

类型是什么意

Kotlin 方法重载

为什么我在使用 Jetpack Compose clickable-Modifier 时收到后端内部错误:Exception during IR lowering error?

如何从定义它们的类外部调用扩展方法?

如何在 Kotlin 中创建一个打开新活动(Android Studio)的按钮?

Kotlin 创建snackbar

未在IntelliJ IDEA上运行临时文件

如何将 Kotlin 日期中的字符串或时间戳格式化为指定的首选格式?

如何为 Java 调用者声明返回类型为void的 Kotlin Lambda?

在 Kotlin 函数上使用 Mokito anyObject() 时,指定为非 null 的参数为 null

可以在函数参数中使用解构吗?