Tag: 构造函数

我如何复制多个主构造函数?

我不确定Kotlin在这方面的最佳做法是什么。 说我有一个Java类, User有两个字段: username和password 。 它有一个这样的主要构造函数: public User(String username, String password) { this.username = username; this.password = hashPassword(password); } 和ORM的第二个构造函数: public User(String username, String password) { this.username = username; this.password = password; } (加上更多的领域没有显示) 通过这个设置,我可以给大部分代码提供一个友好的面孔,并让ORM通过所有的字段来从数据库中重新创建对象。 我的Kotlin代码有一个主要的构造函数: class User(var username: String, var name: String, password: String) 用一个初始值设定项来调用hashPassword并将其分配给一个私有属性。 我怎样才能正确地构造一个二级构造函数,这样我就不必散列来自数据库的值了?

你如何编写types参数的类的二级构造函数?

假设我有下面的类,并带有一个types参数T(在这个例子中它是有界的,以帮助说明后面的例子,但是当它是无界的时候,错误仍然存​​在): class GenericsTest(private var cs: T) 现在假设我想为这个类添加一个辅助构造函数。 我怎样才能做到这一点? 我第一次(天真)的尝试导致了一个编译器错误: class GenericsTest(private var cs: T) { // dummy exists to ensure the method signatures are different constructor(cs: String, dummy: Int) : this(“a”) } IntelliJ强调”a”与消息: Type mismatch. Required: T Found: String 对我来说, String似乎是一个完全有效的T 我想明确指定types参数会有所帮助,但似乎不被允许。 这两种尝试都是不正确的语法: constructor(cs: String, dummy: Int) : this(“a”) constructor(cs: U, dummy: Int) : this(“a”) […]

使用Kotlin自定义Android视图

我正在尝试在我的Android项目中使用Kotlin。 我需要创建自定义视图类。 每个自定义视图有两个重要的构造函数: public class MyView extends View { public MyView(Context context) { super(context); } public MyView(Context context, AttributeSet attrs) { super(context, attrs); } } MyView(Context)用于在代码中实例化视图,当从XML扩充布局时, MyView(Context, AttributeSet)由布局inflater调用。 对这个问题的回答建议我使用默认值或工厂方法的构造函数。 但是,这里是我们所拥有的: 工厂方法: fun MyView(c: Context) = MyView(c, attrs) //attrs is nowhere to get class MyView(c: Context, attrs: AttributeSet) : View(c, attrs) { … } 要么 fun […]

Kotlin二级构造函数

如何在Kotlin中声明次级构造函数? 有没有关于这个的文件? 以下不编译… class C(a : Int) { // Secondary constructor this(s : String) : this(s.length) { … } }

构造函数中的调用顺序是有保证的

假设我有一个具有主构造函数的Kotlin(data)类和一个用调用某些方法来填充主构造函数的val / vars的辅助函数(请参阅示例)。 我的问题是这些方法被调用的顺序是否有保证,即在我的例子中是否在extractNames之前总是会调用checkConsitency 。 data class LawAndOrder(val sherifs: Int, val names: List) { constructor(westerners: List) : this(sherifs = westerners.checkConsistency(), names = westerners.extractNames()) } data class Westerner(val name: String) { val isCriminal: Boolean get() = name == “Jesse James” } private fun List.checkConsistency(): Int { println(“Checking consistency”) if (isEmpty()) throw IllegalArgumentException(“Crime and chaos”) if (any […]

Kotlin:我如何避免构造函数中的代码重复?

通常我发现自己处于一个拥有大量可选参数的超类的情况,这些相同的参数在其子类中也需要是可选参数。 例如,超类: abstract class Plugin(val name: String, val version: String = “1.0”, val author: String = “”, val description: String = “”) 扩展这个类是一个痛苦。 这是一个例子子类: abstract class CyclePlugin(name: String, version: String = “1.0”, author: String = “”, description: String = “”, val duration: Int, val durationUnit: TimeUnit = MILLISECONDS) : Plugin(name, version, author, description) 注意:我将用我的解决方案回答这个问题。 我正在寻找更好的解决方案。

初始块位置在Kotlin上课

我最近遇到了一种情况,我的标准variables的值被默认值所替代,即使我已经使用init块的构造函数赋值了。 我试过的是: class Example(function: Example.() -> Unit) { init { function() } var name = “default name” } // assigning it like this: val example = Example { name = “new name” } // print value print(example.name) // prints “default name” 在挣扎了一下之后,我发现init块的位置很重要。 如果我把init块放在类的最后一个,它首先用缺省值初始化名称,然后调用用“new name”替换值的函数()。 如果我把它放在第一,它没有find名称,它被替换为“默认名称”时,属性被初始化。 这对我来说很奇怪 任何人都可以解释为什么发生这种事

在构造函数中调用super并在Kotlin中分配一个字段?

我想初始化status字段里面的构造函数除了调用super 。 class MyException : RuntimeException { init { val status: Status } constructor(status: Status) : super() constructor(status: Status, cause: Throwable) : super(cause) } 我怎样才能做到这一点?

Kotlin在声明之前初始化一个variables?

这是我的测试代码: class Test { init { a = 1 } constructor() { a = 2 } private var a: Int init { a = 3 } } 如果我删除了第二个构造函数: class Test { init { a = 1 // Error: Variable cannot be initialized before declaration } // constructor() { // a = 2 // } private […]

如何在Kotlin中扩展android.widget.FrameLayout(RequiresApi 21,但是我需要使用minSdk 19)

我试图扩展在我的课kotlin android.widget.FrameLayout。 问题是,因为我升级到kotlin 1.2.21我无法编译,因为FrameLayout的最长(4参数)构造函数需要minSdk 21,但我的库也需要工作在api级别19。 我已经放弃了使用@JvmOverloads(它似乎是越野车和即时运行崩溃),所以我试图写“长”的代码。 就像是: 我尝试的第一种方法: class PullDownAnimationLayout : FrameLayout, Animator.AnimatorListener, PullDownAnimation { @RequiresApi(Build.VERSION_CODES.LOLLIPOP) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) constructor(context: Context) : this(context, null) override val MAX_PULL_HEIGHT_PX: […]