Get field names list from case class

后端 未结 6 1721
别跟我提以往
别跟我提以往 2020-11-27 03:31

I need to get only field names of case class. I\'m not interested in its values. I thought getClass.getDeclaredFields.map(_.getName) would return a list of fiel

6条回答
  •  没有蜡笔的小新
    2020-11-27 04:38

    User.getClass does not give you the equivalent of User.class in Java, but it gives you the class of the companion object of the User class. You can retrieve the Class object of User with classOf[User].

    edit: Oh and the User$.MODULE$ is an accessor to the singleton instance that is used internally. Think of it as the equivalent to MyClass.INSTANCE when you are writing singletons in Java.

提交回复
热议问题