gpt4 book ai didi

class - BodyKt $ main $ Person @ 35cabb2a是什么?我可以得到它的属性吗?

转载 作者:行者123 更新时间:2023-12-02 13:35:35 24 4
gpt4 key购买 nike

我创建类的对象并尝试将其复制。

我试图获取对象的属性,但得到了这一点:

Properties of Ann: Name: Ann Age: 10 Type: man High: 0.5 Code: 1408
Properties of Bob: Name: Bob Age: 20 Type: girl High: 15.0 Code: 1239
Default: Name: Orig Age: 18 Type: piece High: 25.7 Code: 1522
Bob has Anns' props: Name: Ann Age: 10 Type: man High: 0.5 Code: 1408
Default list: [BodyKt$main$Person@35cabb2a]

码:
fun main() {
// copy object in object
class Person (var name: String = "Orig", var type: String = "piece",
var age: Int = 18, var high: Double = 25.7, var code: Int = 1522)
{
var info: String = "0"
get() = "Name: $name Age: $age Type: $type High: $high Code: $code"
val somebody
get () = listOf(Person("Copy", "some", 15, 11.1, 1870))
}
var default: Person = Person()
val ann: Person = Person("Ann", "man", 10, 0.5, 1408) // 1st object with some properties
var bob: Person = Person("Bob", "girl", 20, 15.0, 1239) // 2nd object without prop
println("Properties of Ann: ${ann.info}") // props 1st object
println("Properties of Bob: ${bob.info}") // props 2nd object
println("Default: ${default.info}") // props default
bob = ann.deepCopy() // copy 1st in 2nd
println("Bob has Anns' props: ${bob.info}") // new props 2nd object
println("Default list: ${default.somebody}")
}

默认列表: [BodyKt$main$Person@35cabb2a]是什么?

最佳答案

这是the default toString implementation的结果,因为您没有为Person覆盖它:BodyKt$main$Person是JVM类名称,因为它是在main中定义的。

看来您的info应该只替换为toString的替代:

fun main() {
// copy object in object
class Person (var name: String = "Orig", var type: String = "piece",
var age: Int = 18, var high: Double = 25.7, var code: Int = 1522)
{
override fun toString() = "Name: $name Age: $age Type: $type High: $high Code: $code"
val somebody
get () = listOf(Person("Copy", "some", 15, 11.1, 1870))
}
var default: Person = Person()
val ann: Person = Person("Ann", "man", 10, 0.5, 1408) // 1st object with some properties
var bob: Person = Person("Bob", "girl", 20, 15.0, 1239) // 2nd object without prop
println("Properties of Ann: $ann") // props 1st object
println("Properties of Bob: $bob") // props 2nd object
println("Default: $default") // props default
bob = ann.deepCopy() // copy 1st in 2nd
println("Bob has Anns' props: $bob") // new props 2nd object
println("Default list: ${default.somebody}")
}

关于class - BodyKt $ main $ Person @ 35cabb2a是什么?我可以得到它的属性吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956063/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com