示例代码
/**
* 定义一个泛型类
*/
abstract class BaseResponseBean<T> {
var code: Int = 0
var msg: String? = null
var data: T? = null
override fun toString(): String {
return "code=$code msg=$msg data=$data"
}
}
/**
* 定义一个泛型接口
*/
interface TestInterface<T> {
fun test(t: T)
}
/**
* 继承泛型类、实现泛型接口
*/
class ResponseBean : BaseResponseBean<String>(),TestInterface<String> {
override fun test(t: String) {
println(t)
}
}
fun main(args: Array<String>) {
val responseBean = ResponseBean()
responseBean.code = 200
responseBean.msg = "获取成功"
responseBean.data = "返回数据"
println(responseBean.toString())
responseBean.test("执行test方法")
}
执行结果:
code=200 msg=获取成功 data=返回数据
执行test方法