在Scala中,如何获得对象(而不是类的实例)的名称? [英] In Scala, how do I get the *name* of an `object` (not an instance of a class)?

查看:74
本文介绍了在Scala中,如何获得对象(而不是类的实例)的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Scala中,我可以这样声明一个对象:

In Scala, I can declare an object like so:

class Thing

object Thingy extends Thing

我将如何获得 Thingy >(对象名称)在Scala中?

How would I get "Thingy" (the name of the object) in Scala?

我听说,Lift(Scala的网络框架)可以做到这一点。

推荐答案

只需获取类对象,然后获取其名称。

Just get the class object and then its name.

scala> Thingy.getClass.getName
res1: java.lang.String = Thingy$

所有这些左侧是删除 $

编辑:

要删除封闭对象的名称和尾部 $ ,只需这样做

To remove names of enclosing objects and the tailing $ it is sufficient to do

res1.split("\\$").last

这篇关于在Scala中,如何获得对象(而不是类的实例)的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆