或许可以稍微解释一下。
假设我有一个名为Page的抽象类,它看起来像这样:
abstract class Page {
abstract fun title() : String
}
还有一个名为“Book”的接口:
interface Book {
fun write(page: Page)
fun read(title: String) Page
}
我的问题是如何使用插入符号泛型语法来指定这些方法必须由Page类的派生实例使用。可能是这样的:
interface Book<Page> {
fun write(page: Page)
fun read(title: String) : Page
}
class AdventureBookPage(val pageTitle: String, val content: String) : Page() {
override fun title() : String {
return title
}
}
class AdventureBook : Book<AdventureBookPage> {
override fun write(abp: AdventureBookPage) {
// do writing ops
}
override fun read(title: String) : AdventureBookPage {
// do read ops
}
}
我对泛型的工作方式理解错了吗?非常感谢任何帮助。