在Java中
我可以实现两个类似的构造函数
public TargetTitleEntryController() { }
public <T extends Controller & TargetTitleEntryControllerListener> TargetTitleEntryController(T targetController) {
setTargetController(targetController);
}
我想将其转换为Kotlin
class TargetTitleEntryController ()
使用辅助构造函数。我不知道如何用Java对应的泛型类型声明。
答案 0 :(得分:2)
科特林(悲伤)没有路口类型
但是有Generic constraints(希望)
但是Generic constraints不适用于辅助构造函数(悲伤)
但是您可以使用Invoke operator overloading在替代对象中模拟辅助构造函数(解决方法):
>>> import sqlite3
>>> def create_user_table():
... connection = sqlite3.connect("test.db")
... c = connection.cursor()
... c.execute('''CREATE TABLE IF NOT EXISTS users
... (Username TEXT PRIMARY KEY,Password TEXT NOT NULL, RiotID TEXT)''')
... c.execute('''CREATE TABLE IF NOT EXISTS RANK
... (Userid TEXT PRIMARY KEY, CurrentMMR INTEGER NOT NULL, FOREIGN KEY (Userid) REFERENCES users (Username))''')
... connection.commit()
... connection.close()
... print("table created")
...
>>> create_user_table()
table created
>>>
>>> import os
>>> os.system("sqlite3 test.db .schema")
CREATE TABLE users
(Username TEXT PRIMARY KEY,Password TEXT NOT NULL, RiotID TEXT);
CREATE TABLE RANK
(Userid TEXT PRIMARY KEY, CurrentMMR INTEGER NOT NULL, FOREIGN KEY (Userid) REFERENCES users (Username));
0
>>>
答案 1 :(得分:1)
在此示例中,您指定实现两个接口(T
,CharSequence
)的类型Runnable
:
class Person<T>(val name: String) where T : CharSequence, T : Runnable {
constructor(name: String, parent: T) : this(name) {
}
}
实际上,这样的事情应该起作用:
class TargetTitleEntryController<T> () where T : Controller, T : TargetTitleEntryControllerListener {
constructor(targetController: T) : this() {
}
}
答案 2 :(得分:0)
您可以这样做:)
class TargetTitleEntryController <T>() : Controller() where T: Controller, T: TargetTitleEntryControllerListener<T> {
constructor(target: T) : this() {
targetController = target
}
}
您可以像这样在父控制器中实现它:
class TargetDisplayController : Controller(), TargetTitleEntryControllerListener<TargetDisplayController> {
var targetTitleEntryController = TargetTitleEntryController(this)
override fun onTitlePicked(String option) {
}
override fun onAttach(view: View) {
// push controller here
}
}