【问题标题】:using Enum in traits in scala在scala的特征中使用枚举
【发布时间】:2015-06-08 06:19:12
【问题描述】:

我有一个名为 UserT 的特征和一个扩展该特征的类 DirectUserT 我想在特征中添加枚举,以便子类可以使用它 我制作了一个扩展枚举的 scala 对象 UserStatus 现在我想在我的 trait 中加入这个枚举,以便子类可以使用它,但我不知道该怎么做?

我的枚举对象

package testlogic

    object UserStatus extends Enumeration{

        type  UserStatus = Value
        val ACTIVE , INACTIVE , BLOCKED , DELETED = Value

    }

这是我的 UserT 代码

package testlogic
 import testlogic.UserStatus._
trait UserT {

  var name : String = ""
  def setName( aName: String)= {
    name = aName
  }
  def getName : String = {
    name
  }

}

DirectUserT.scala

package testlogic


    class DirectuserT extends  UserT {

     var currentStatus =BLOCKED
     //println(currentStatus)

    }

eclipse 在 BLOCKED 上显示错误

请帮忙

【问题讨论】:

    标签: scala enums


    【解决方案1】:

    你需要添加

    import testlogic.UserStatus._
    

    给你上课 DirectUserT.scala

    或者在你的特质中添加它:

    trait UserT {
      import testlogic.UserStatus._
    }
    

    【讨论】:

    • 请告诉我这是正确的方法吗?我的意思是在这种情况下它不是从特征继承枚举它只是直接导入枚举并使用它?
    • 您正在使用枚举“内部”定义的值。因此我们应该这样做。或直接 UserStatus.BLOCKED
    猜你喜欢
    • 2015-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多