具有超类的Kotlin构造函数 [英] Kotlin constructor with super class
问题描述
无论尝试哪种方式,无论是主要构造方法还是次要构造方法,我都无法弄清楚如何用kotlin中的超类和构造方法声明新类.
Any way I try, be it primary constructors or secondary, I can't figure out how to declare a new class with a super class and a constructor in kotlin.
class myPanel : JPanel {
myPanel() : super() {
}
}
这是我最想做的事情,但它给出了一个错误,期望成员声明.
This is how i would most like t do it but it gives an error expecting member declaration.
class myPanel() : JPanel() {
{
...
}
}
这是我认为主要构造函数的外观,但是它给出了相同的错误.搜索互联网并没有帮助,我能找到的只是第二个例子.
this is how I thought a primary constructor looked but it gives the same error. Searching the internet hasn't been helpful and all I could find was the second example.
那么,使用超类及其一个构造函数创建类的所有有效方法是什么?
So, what are all of the valid ways of creating a class with a super class and its one constructor?
推荐答案
Kotlin的构造函数包含在init
块中
Kotlin's constructor is contained in an init
block
class Test : SuperClass() {
init {
// Do constructor stuff here
}
}
更多信息可以在类的Kotlin参考中找到: https://kotlinlang.org/docs/reference /classes.html#constructors
More information can be found in the Kotlin reference on classes: https://kotlinlang.org/docs/reference/classes.html#constructors
这篇关于具有超类的Kotlin构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!