在深度嵌套数据类中调用 getter 的更优雅方法
我已经反序列化了一个巨大的 XML 文件,这给我留下了很多(嵌套的)数据类。我正在寻找一种更优雅的方法来调用这些对象中的特定 getter。所以我不想用所有这些点调用来填充我的代码。
我必须设置相当多的字段来返回一个新模型,目前看起来像这样(我为这个问题编写了模型):
return TestClass(
appointmentDate = message.data.appointment.appointmentDescription.AppointmentDetails.appointmentDate
appointmentX = message.data.appointment.appointmentDescription.X.Y.Z
appointmentY = message.data.appointment.appointmentDescription.AppointmentDetails.X.Y.Z
appointmentZ = message.data.appointment.Z
)
我知道我可以执行 val AppointmentDescription = message.data.appointment.appointmentDescription 并从那里可以缩短一点,但我认为一定有一个更短更好的方法。
有谁知道如何以更优雅和干净的方式做到这一点?我对 Kotlin 还很陌生,我很难在互联网上正确查找任何解决方案,因为我找不到任何解决方案,并且可能正在寻找错误的东西。
I have deserialized a huge XML file and that leaves me with a lot of (nested) data classes. I'm looking for a more elegant way to do calls to specific getters within these objects. So I would rather not fill my code with all these dot calls.
I have to set quite some fields to return a new Model, which currently looks something like this (I made up the model for this question):
return TestClass(
appointmentDate = message.data.appointment.appointmentDescription.AppointmentDetails.appointmentDate
appointmentX = message.data.appointment.appointmentDescription.X.Y.Z
appointmentY = message.data.appointment.appointmentDescription.AppointmentDetails.X.Y.Z
appointmentZ = message.data.appointment.Z
)
I know I could do val appointmentDescription = message.data.appointment.appointmentDescription and do my calls from there to shorten it a bit, but there must be a shorter and better way I assume.
Does anyone know a way to do this in a more elegant and clean way? I'm pretty new to Kotlin and it is quite hard for me to properly lookup any solutions on the internet since I can't find any and am probably searching for the wrong things.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用
with
函数:这应该很有用:https://medium.com/mobile-app-development-publication/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84
Try the
with
function:This should be useful: https://medium.com/mobile-app-development-publication/mastering-kotlin-standard-functions-run-with-let-also-and-apply-9cd334b0ef84