我有一个类似下面的通用方法
private fun <T> getSomething(): T {
return "something" as T
}
如何使用变量T
类型调用此方法?
val types = arrayListOf<Type>(String::class.java, Boolean::class.java)
types.forEach { type ->
val something = getSomething<type>() // Unresolved reference: type
}
在运行时,我不知道泛型T
是什么。我从types
获取类型,应该使用通用的getSomething
方法传递它。
我要调用具有多个表的数据库。示例模型就是这样
class User{
}
class Student{
}
由于所有调用查询基本相同,因此我想拥有用于调用数据库和获取数据的通用方法。
private fun <T> getData(model: String): List<T>?{
return when(model){
"user" -> getUsers()
"student" -> getStudents()
else -> null
}
}
所以当我调用上述方法时。在我的循环中,我想将Type
传递为User
或Student
。
val types = arrayListOf<Type>(User::class.java, Student::class.java)
types.forEach { type ->
val data = getData<type>(type.javaClass.simpleName) // Unresolved reference: type in <type>
}
我如何实现它。
答案 0 :(得分:1)
这是一个完整的例子:
import kotlin.reflect.KClass
data class User(val name: String)
data class Student(val name: String)
fun getUsers(): List<User> = listOf(User("JB"))
fun getStudents(): List<Student> = listOf(Student("Claire"))
fun <T: Any> getData(clazz: KClass<T>): List<T>? {
return when(clazz) {
User::class -> getUsers() as List<T>
Student::class -> getStudents() as List<T>
else -> null
}
}
fun main(args: Array<String>) {
val types = listOf(User::class, Student::class)
types.forEach { type ->
val data = getData(type)
println(data)
}
}
答案 1 :(得分:0)
我会坚持使用像
这样的具体类型import kotlin.reflect.KClass
interface IBaseData
interface IDataTable<out T> where T : IBaseData
{
fun getData(): List<T>
}
class User : IBaseData
class Student : IBaseData
class UserTable : IDataTable<User>
{
override fun getData(): List<User>
{
return listOf(User())
}
}
class StudentTable : IDataTable<Student>
{
override fun getData(): List<Student>
{
return listOf(Student())
}
}
inline fun <reified T: IBaseData> getDataTable() : IDataTable<T>?
{
return when(T::class)
{
User::class -> UserTable() as IDataTable<T>
Student::class -> StudentTable() as IDataTable<T>
else -> null
}
}
fun main()
{
var user = getDataTable<User>()?.getData()
var student = getDataTable<Student>()?.getData()
}
但是,这仍然是开销,为什么不直接使用getUser或getStudents