Parcelable派生类在科特林
我使用的是从https://github.com/nekocode/android-parcelable-intellij-plugin-kotlinParcelable派生类在科特林
我这个定义
class ChessClock : TextView {
lateinit var player:String
constructor(context: Context) : super(context)
constructor(context:Context, p:String, angle:Float) : this(context) {
player = p
rotation = angle
}
<snip>
}
和定义改为
class ChessClock() : TextView, Parcelable {
lateinit var player:String
constructor(context: Context) : super(context)
constructor(context:Context, p:String, angle:Float) : this(context){
player = p
rotation = angle
}
<snip -- various stuff added here>
}
试图在类的Android Parcelable插件突出显示了两个语法错误。
在行
class ChessClock() : TextView, Parcelable
TextView
有下划线与评论“这种类型有一个构造函数,并且必须在这里初始化。”
在行
constructor(context: Context) : super(context)
super
有下划线与评论“主构造函数调用的预期。”
我只用了kotlin几个星期,我不明白这里发生了什么。首先,我知道(或至少我认为我知道)科特林没有实现多重继承,所以我不明白是什么
类ChessClock():TextView的,Parcelable
手段。这真的是合法的kotlin吗?如何在kotlin中创建派生类Parcelable?
- TextView的一类,因此你的主构造应该 调用它的构造函数之一
- ,则应该从其他构造
实例调用你的主构造:
class ChessClock(context: Context) : TextView(context), Parcelable
//in this case you don't need other constructors but in-case you do, this is how you should write it:
constructor(context: Context, dummy: Int): this(context)
你也可以将生成的代码从ChessClock()
更改为ChessClock
。
但这不会帮助你解决'构造函数(parcel:Parcel)'问题,请参阅我的[回答你的其他问题](https://stackoverflow.com/questions/45578982/constructor-taking-parcelable-constructor- in-kotlin-derived-class/45579620#45579620)...但简短的答案是不要试图让你的UI可以被分类,如果可以的话,将UI与支持数据分开。 – Les
完美。谢谢。 – saulspatz
糟糕。我说得太快了。现在我看到编译器抱怨声明'constructor(parcel:Parcel):this()'。该声明应该如何改变? – saulspatz
我打算让这个问题成为一个新问题。 – saulspatz