问题描述
我想编写一个函数,该函数需要使用类声明的类表示形式,例如MyClass::class.java
.
I want to write a function, which needs to use a the class representation of a class declaration like MyClass::class.java
.
我有一个基类ActivityStarter
,它用作活动中伴侣对象的基础.
I have a base class ActivityStarter
which I use as a base for companion objects in activities.
// declarations
open class ActivityCompanion<T: AppCompatActivity>(val activityClass : Class<T>) {
fun startActivity(context: Context) {
context.startActivity(Intent(context, activityClass))
}
}
class MyActivity : AppCompatActivity() {
companion object : ActivityStarter<MyActivity>(MyActivity::class:java)
...
}
// call
MyActivity.startActivity(this)
在声明以及伴随对象的继承中,该类基本上传递了两次.一次作为类型参数,一次作为常规参数. (ActivityStarter<MyActivity>(MyActivity::class:java)
).
In the declaration, and the inheritence of the companion object, the class is basically passed twice. Once as type parameter and once as normal parameter. (ActivityStarter<MyActivity>(MyActivity::class:java)
).
参数activityClass
是必需的,因为我不能使用T::class.java
. 不能将T用作化类型参数.使用类代替".
The parameter activityClass
is necessary, because I cant use T::class.java
. "Cannot use T as reified type parameter. Use class instead".
我曾经只是在函数调用中传递参数:
I have used to just pass the parameter in the function call:
// declarations
open class ActivityStarter {
inline fun <reified T : AppCompatActivity>startActivity(context: Context) {
context.startActivity(Intent(context, T::class.java))
}
}
class MyActivity : AppCompatActivity() {
companion object : ActivityStarter()
...
}
// call
MyActivity.startActivity<MyActivity>(this)
这从同伴对象声明中删除了冗余,但将其放入了调用中,并基本上使类中的type参数变得无用.
This removes the redundancy from the companion object declaration but puts it into the call and basically makes the type parameter in the class useless.
是否有一种方法可以使类声明reified
中使用的类型参数,以便可以实现这样的实现?:
Is there a way to make the type parameter used in the class declaration reified
, so that I can have an implementation like this?:
// declarations
open class ActivityCompanion<reified T: AppCompatActivity>() {
fun startActivity(context: Context) {
context.startActivity(Intent(context, T::class.java))
}
}
class MyActivity : AppCompatActivity() {
companion object : ActivityStarter<MyActivity>()
...
}
// call
MyActivity.startActivity(this)
或者是另一种取消activityClass参数的方式,这样我就可以拥有这样的东西:
Or another way to resctict the activityClass parameter so that I can have something like this:
// declarations
open class ActivityStarter(private val activityClass : Class<T : AppCompatActivity>) {
inline fun startActivity(context: Context) {
context.startActivity(Intent(context, activityClass))
}
}
class MyActivity : AppCompatActivity() {
companion object : ActivityStarter(MyActivity::class.java)
...
}
// call
MyActivity.startActivity(this)
推荐答案
不.但是,如果您使startActivity
不是ActivityCompanion
的成员,而是一个扩展函数,以便它 可以具有化类型参数,该怎么办?
No. But what if you make startActivity
not a member of ActivityCompanion
, but an extension function so it can have reified type parameters?
class ActivityCompanion<T: AppCompatActivity>()
fun <reified T: AppCompatActivity> ActivityCompanion<T>.startActivity(context: Context) {
context.startActivity(Intent(context, T::class.java))
}
class MyActivity : AppCompatActivity() {
companion object : ActivityStarter<MyActivity>()
...
}
MyActivity.startActivity(this)
如果我理解正确,并且您希望ActivityStarter
接受AppCompatActivity
的任何子类而没有类型参数本身,则可以将其编写为
If I understood correctly and you want ActivityStarter
to accept any subclass of AppCompatActivity
without having a type parameter itself, you can write it as
open class ActivityStarter(protected val activityClass: Class<out AppCompatActivity>) { ... }
请参见官方文档或,表示out
的含义.
See official documentation or What is out keyword in kotlin for the meaning of out
.
这篇关于使用类声明中的类型参数,例如函数中的类型化参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!