android - 为什么 BaseAdapter 不需要括号?
问题描述
我在问为什么我不能写这个:
class myClass : BaseAdapter() {
}
而不是这个:
class myClass : BaseAdapter {
}
BaseAdapter 是一个接口。
解决方案
BaseAdapter
是一个abstract
类,而不是一个interface
. 在 Kotlin 中,如果你从一个类(abstract
或其他)扩展,你需要使用构造函数调用。
如果你从这个开始:
import android.widget.BaseAdapter
class myClass : BaseAdapter() {
}
...你会得到一个错误:
那是因为您缺少abstract
课程所需的功能。如果您将文本光标放在class myClass
错误区域并按Alt-Enter,您可以选择“实现方法”,选择所有四个方法,Android Studio 将为TODO()
您生成这些方法的实现代码:
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class myClass : BaseAdapter() {
override fun getView(
position: Int,
convertView: View?,
parent: ViewGroup?
): View {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItem(position: Int): Any {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getItemId(position: Int): Long {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
override fun getCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
}
}
此时,您应该没有更多错误。
推荐阅读
- javascript - 从数据数组发送恒定大小的批处理请求 - React,Axios
- sql - 需要在 DB2 Query 中获取直到小数点后 15 位精度的值
- haskell - Haskell 中的依赖地狱 (cabal, uu-parsinglib)
- reactjs - 关于反应和表达网站并更新它们的问题
- python-3.x - python补丁没有修补测试的类
- javascript - Firebase“未处理的拒绝”和“设置标头后无法设置标头”JavaScript 错误
- oracle-apex - Oracle Apex:如何翻译消息:您的会话已过期?
- symfony - Symfony 5 LDAP JumpCloud 身份验证
- java - KMP DFA 重启状态
- python-3.x - 格式化输出(python)