为什么要在Kotlin的班级会员财产反思?
在“ Kotlin In Action”中,它说:“如果成员Property指的是人类班级的年龄属性,则会成员Property.get(Person)是一种动态获取人的价值的方式。反射API):
class Peron(val name: String, val age: Int)
>> val person = Person("Alice", 29)
>> val memberProperty = Person::age
>> println(memberProperty.get(person))
29
我不明白为什么此示例是指“动态”获得属性的价值。当我运行此代码时,它只是有效的:
println(person.age)
是否还有其他成员属性反射的情况或示例?
In 'Kotlin in Action', it says "if a memberProperty refers to the age property of the Person class, memberProperty.get(person) is a way to dynamically get the value of person.age" with code(10.2.1 The Kotlin Reflection API):
class Peron(val name: String, val age: Int)
>> val person = Person("Alice", 29)
>> val memberProperty = Person::age
>> println(memberProperty.get(person))
29
I can't understand why this example refers to "dynamically" getting the value of property. It just works when I run this code:
println(person.age)
Is there any other case or example of member property reflection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
例如,假设您要编写一个函数,该函数及其值及其值以及您可以做到这一点:
用法:
输出:
For example, say you want to write a function which prints all the properties of an object along with their values, this is how you can do that:
Usage:
Output: