如何在 kotlin 中设置已创建对象的 属性?
How to set a property of a created object in kotlin?
我在该站点上看到许多不同的类似问题都引用了这个问题,但所有问题的目的都是从 class 本身内部创建 属性。我一直在查看 kotlin 文档一段时间,但似乎找不到解决方案。我正在尝试接受一个随机对象并为其添加一个属性。
//create this object
class random(val name : String?){
}
var obj = random("bob")
obj.newattribute :Int = 2 //cant do this in this lang
您的意图似乎是在运行时通过向 class 的实例添加新属性来操纵 class。
如果您阅读 documentation of Kotlin classes,您会偶然发现以下段落(链接文档中的第一段):
Kotlin's object model is substantially different from Python's. Most importantly, classes are not dynamically modifiable at runtime! (There are some limited exceptions to this, but you generally shouldn't do it. However, it is possible to dynamically inspect classes and objects at runtime with a feature called reflection - this can be useful, but should be judiciously used.) All properties (attributes) and functions that might ever be needed on a class must be declared either directly in the class body or as extension functions, so you should think carefully through your class design.
这仅仅意味着这在 Kotlin 中是不可能的,它指出您甚至不应该大量使用运行时检查,这是可能的。
在 Kotlin 中,您不能(也不应该)在运行时更改实例的“形状”,因为类型系统在这里保护用户并为您正在操作的值提供保证。
也就是说,如果您需要一个 key-value 存储,您可以在其中放置 key-value 对并通过它们的键访问值,您应该使用 MutableMap
界面。
例如,HashMap
是该接口的实现:
val myMap = HashMap<String, Int>()
myMap["newattribute"] = 2
println(myMap["newAttribute"]) // prints "2"
请注意,键和值的类型已明确定义。给定的 Map
只能处理相同类型的键(此处为 String
s)和相同类型的值(此处为 Int
s)。这意味着您不能对上面示例中定义的地图执行 myMap["stringProperty"] = "text"
。
如果您真的想要更通用的东西(尤其是对于值),则需要使用更宽的类型作为地图中的“值”类型。例如,改用 MutableMap<String, Any>
,以便值可以是任何 non-null 类型:
val myMap = HashMap<String, Any>()
myMap["intAttr"] = 42
myMap["strAttr"] = "text"
println(myMap["intAttr"]) // prints "42"
println(myMap["strAttr"]) // prints "text"
我在该站点上看到许多不同的类似问题都引用了这个问题,但所有问题的目的都是从 class 本身内部创建 属性。我一直在查看 kotlin 文档一段时间,但似乎找不到解决方案。我正在尝试接受一个随机对象并为其添加一个属性。
//create this object
class random(val name : String?){
}
var obj = random("bob")
obj.newattribute :Int = 2 //cant do this in this lang
您的意图似乎是在运行时通过向 class 的实例添加新属性来操纵 class。
如果您阅读 documentation of Kotlin classes,您会偶然发现以下段落(链接文档中的第一段):
Kotlin's object model is substantially different from Python's. Most importantly, classes are not dynamically modifiable at runtime! (There are some limited exceptions to this, but you generally shouldn't do it. However, it is possible to dynamically inspect classes and objects at runtime with a feature called reflection - this can be useful, but should be judiciously used.) All properties (attributes) and functions that might ever be needed on a class must be declared either directly in the class body or as extension functions, so you should think carefully through your class design.
这仅仅意味着这在 Kotlin 中是不可能的,它指出您甚至不应该大量使用运行时检查,这是可能的。
在 Kotlin 中,您不能(也不应该)在运行时更改实例的“形状”,因为类型系统在这里保护用户并为您正在操作的值提供保证。
也就是说,如果您需要一个 key-value 存储,您可以在其中放置 key-value 对并通过它们的键访问值,您应该使用 MutableMap
界面。
例如,HashMap
是该接口的实现:
val myMap = HashMap<String, Int>()
myMap["newattribute"] = 2
println(myMap["newAttribute"]) // prints "2"
请注意,键和值的类型已明确定义。给定的 Map
只能处理相同类型的键(此处为 String
s)和相同类型的值(此处为 Int
s)。这意味着您不能对上面示例中定义的地图执行 myMap["stringProperty"] = "text"
。
如果您真的想要更通用的东西(尤其是对于值),则需要使用更宽的类型作为地图中的“值”类型。例如,改用 MutableMap<String, Any>
,以便值可以是任何 non-null 类型:
val myMap = HashMap<String, Any>()
myMap["intAttr"] = 42
myMap["strAttr"] = "text"
println(myMap["intAttr"]) // prints "42"
println(myMap["strAttr"]) // prints "text"