我在语言定义中找不到任何东西来解释Kotlin中类的初始化。
import java.util.Properties
fun main(args: Array<String>) {
val out = MyClass()
out.fn()
}
class MyClass {
private val a = Properties() // 1
init {
fn()
}
public fun fn() {
println("Fn called. a = $a")
}
class Animal {
val name: String
constructor(name: String){
this.name = name // initialized via constructor
}
}
对于Kotlin中的上述类,我可以通过辅助构造函数初始化val属性,但数据类不允许这样做。
data class User(val name: String, val postalCode: Int) {
val email: String
constructor( email: String): this("
因为我有一个有两个参数的User类: first_name,last_name。所以我使用be的kotlin类:
data class User(val first_name:String, val last_name:String)
现在我想要一个只接受first_name的构造函数,或者你可以说只接受一个参数。我如何用Kotlin定义它?
我知道我们可以传递默认值,这样我们就可以忽略第二个参数,但是我们如何编写多个构造函数呢?
我已经开始学习Kotlin了。我想知道init块和constructor之间的区别。这与我们如何利用这一点来改进之间有什么区别呢?
class Person constructor(var name: String, var age: Int) {
var profession: String = "test"
init {
println("Test")
}
}
我想知道是否有任何方法可以引用Kotlin构造函数中先前定义的属性。类似于:
data class Order(
val id: String,
val transformedId: String
}
然后,在启动类时,执行以下操作:
val orderId = getOrderId()
Order(
id = orderId,
transformedId = transform(id)
}
我有一个Kotlin数据类,它接受下面的参数和parcelable参数。我在这个数据类中定义了一个辅助构造函数,它有两个附加参数lastSeen和netTotal。但是,我一直得到错误--在代理中有一个循环,为辅助构造函数调用chain。我该怎么解决这个问题?
@Parcelize
data class Person(
val email: Email,
val overrideEmail: Email,
val phone: String,
val firstName: String,
val overrideFirstName: String,
我正在尝试在recyclerView适配器中创建其他构造函数,但是遇到错误,请告诉我如何在回收器适配器中创建二级构造函数,并在kotlin android studio中使用arraylist参数。
class CustomRecyclerAdapter constructor(
val context1: Context,
val topics: Array<String>,
private var alist: ArrayList<Array<String>>
) : RecyclerView.Adapter<Custom
假设我有下面的类,有一个类型参数T(在这个例子中,它是有界的,以帮助说明后面的例子,但当它是无界的时,错误仍然存在):
class GenericsTest<T : CharSequence>(private var cs: T)
现在假设我想要向这个类添加一个辅助构造函数。我该怎么做呢?我的第一次(幼稚的)尝试导致了一个编译器错误:
class GenericsTest<T : CharSequence>(private var cs: T)
{
// dummy exists to ensure the method signatures are diffe
如何在Kotlin Android中为数据类创建构造函数?
data class EventItem(
@SerializedName("dateEvent")
val dateEvent: String,
@SerializedName("dateEventLocal")
val dateEventLocal: Any,
@SerializedName("idAwayTeam")
val idAwayTeam: String,
@Se
考虑以下类:
open class Parent(foo: Map<a, b>)
class Child(foo: Map<a, b>): Parent(foo)
这在这里似乎是可行的,因为foo的定义在两个类中都是相同的。然而,当我尝试去做
class Child(foo: Map<a, b>? = mapOf()): Parent(foo)
它不再起作用,因为在Child类中foo被定义为可选。这给我带来了问题,因为我试图用Java语言初始化Child类,显然它不允许我有选择地传递像Kotlin这样的参数,所以我想知道在这里我可以做些什么来允许Java
在Java中,您可以声明一个私有的最终成员变量,然后从构造函数中初始化它,这是非常有用的,也是非常常见的事情:
class MyClass {
private final int widgetCount;
public MyClass(int widgetCount) {
this.widgetCount = widgetCount;
}
在Kotlin中,如何使用传递给构造函数的值初始化最终成员变量(val类型)?
我正在学习kotlin,我想了解如何实现从java到kotlin的以下代码:
private Question[] questions = new Question[] {
new Question(R.string.question_a, true)
new Question(R.string.question_b, false)
};
public class Question {
private int resultId;
private boolean answerT;
public Question(int resId, boolean theAns
我想使用kotlin数据类作为例外,这似乎很好:
data class MyException(val extraData: Any) : RuntimeException()
在存在cause的情况下,我还希望能够将它传递给超类。不幸的是,数据类只能在其主构造函数中包含val/var,而且由于默认构造函数调用了no-args RuntimeException()构造函数,我似乎无法做到这一点,而不总是要求传递cause,并将其作为字段存储在类中,这是我不想要的。
我想要的是这样的:
data class MyException(val extraData: Any) : RuntimeExc
这是我的测试代码:
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() {
//
我有以下代码:
class ViewHolder(itemView: View, val ui: ViewUI)
我不希望字段ui有getter,但是kotlin默认生成它,因为它是在构造函数中定义的val。
在中,我发现可以使用以下代码生成私有getter
var setterVisibility: String = "abc"
private set
但我无法在构造函数定义中找到这样的方法
我试图像这样调用父母的第二个构造函数
abstract class A(val i: Int) {
constructor(c: C) : this(c.i)
}
class B() : A(0) {
constructor(c: C) : super(c) // error is here
}
class C(val i: Int)
但是它会产生Primary constructor call expected错误。子类如何调用父类的辅助构造函数?
我有带有参数x和y的B类,它们是从带有参数x的A类扩展而来的,它是可选的(所有参数都是非空的),我如何才能以可选的方式定义B,并且它将在A的构造函数中使用可选值
val y = 0
val b = if (y == 0) B(y) else B(y, 0)
class B(y: Int, x: Int = 238) : A(x)
open class A(x: Int = 238)
在这里,我在B的构造函数中设置了x的默认值。有没有什么方法可以不用在B中设置默认值
我使用的是房间数据库,我想以kotlin元组的形式查询表列。有没有人能帮我一下?
所以我有一个名为User的表,它有多个字段,现在我想查询两个特定的字段first_name和last_name以及Kotlin Tuples(对)
@Entity(tableName = "user")
data class User(
var id: String,
var first_name: String,
var last_name: String
// some other fields
)
@Dao
interface UserDao {
@Quer
我找不到更好的标题来描述如何避免这个Kotlin类中的代码重复(需要表达式):
class Person(email: String) {
var email: String = email
set(value) {
require(value.trim().isNotEmpty(), { "The email cannot be blank" })
field = value
}
init {
require(email.trim().isNotEmpty(),
我是Kotlin的新手,来自JS (但我是一个非常业余的,自学的程序员)。在过去,我的方法是完成它,但是我一直喜欢Kotlin的简洁,并且希望在我的程序中避免不必要的重复。我想知道我是否能够得到一些关于父类的子类的指导。目前,我有两种类型的工作:
class Ratio(var num: Int, var den: Int) {
var monzo: MutableList<Int> = calculateMonzo(num, den)
var sizeInCents: Double = calculateCents(num, den)
var centD
我有以下类型
type NumberedCollection(list) =
let list = list
new () = NumberedCollection(ResizeArray<string>())
它对甜食有好处,就像果冻一样。
但是当我添加另一个构造函数,它接受seq<string>
type NumberedCollection(list) =
let list = list
new () = NumberedCollection(ResizeArray<string>())
new (col :
我正在尝试找到一种在每次测试前设置变量的方法。就像Junit中的@ the方法一样。浏览kotlin-test中的文档,我发现我可以使用interceptTestCase()接口。但不幸的是,下面的代码将触发异常:
kotlin.UninitializedPropertyAccessException: lateinit property text has not been initialized
class KotlinTest: StringSpec() {
lateinit var text:String
init {
"I hope variable is be ini