如何轻松获得Scala案例类的名称? [英] How can I easily get a Scala case class's name?
本文介绍了如何轻松获得Scala案例类的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出:
case class FirstCC {
def name: String = ... // something that will give "FirstCC"
}
case class SecondCC extends FirstCC
val one = FirstCC()
val two = SecondCC()
如何从 one.name $获得
FirstCC
c $ c>和 SecondCC
from two.name
?
How can I get "FirstCC"
from one.name
and "SecondCC"
from two.name
?
推荐答案
def name = this.getClass.getName
或者如果您只想要不带包装的名称:
Or if you want only the name without the package:
def name = this.getClass.getSimpleName
请参见 java.lang.Class 了解更多信息。
这篇关于如何轻松获得Scala案例类的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文