English 中文(简体)
kotlin general category ,the differences with BaseResp<BaseCode.Data> and BaseCode:BaseResp<BaseCode.Data>
原标题:kotlin generic type ,the difference with BaseResp<BaseCode.Data> and BaseCode:BaseResp<BaseCode.Data>
class BaseCode: BaseResp<BaseCode.Data>() {

    inner class Data{
        var code = 0
    }
}

open class BaseResp<T> {
  
    var data:T? = null
}

interface IMessageCallback<T> {

    fun onMessage(data:T)
   
}

inline fun <reified T:BaseResp<Any>> send(cb: IMessageCallback<T>?){
}

fun test(cb:IMessageCallback<BaseCode>?){
      send(cb)
}

the compiler error:the BaseResp<BaseCode.Data> is diffirent with BaseCode:BaseRest<BaseCode.Data>

之后一改如下:

 inline fun <reified T> send(cb: IMessageCallback<BaseResp<T>>?){

 }
 fun test(cb:IMessageCallback<BaseResp<BaseCode.Data>>?){
        send(cb)
 }
  

这样做是正确的,但其他呼吁者却不容易,而且看上去是可疑的。

聪明的画面贯穿这一设想,以及如何解决。

最佳回答

页: 1 要求打电话者通过<代码>BaseResp<Any>或次类。 这样就可以在类似情况下通过。

class Sample: BaseResp<Any>()

but not

class Sample: BaseResp<String>()

In short, Fee<T>: Foo<T> works, but Fee<X:T>: Foo<T> won t.

您将<代码>改为

inline fun <reified T:BaseResp<out Any>> send(cb: IMessageCallback<T>?)

it ll work. Note the out Any - this is a covariant type parameter (and is called "Type Projection"), and treats a BaseResp<String> (or any other type in place of String) as a subtype of BaseResp<Any>.

Now you can use BaseResp<String> as well as Sample.

Note that because we ve defined the type of data as out in the declaration of send, you can only read the data from the BaseResp inside send, you can t write it. This restriction makes it possible to pass in subtypes of that parameter.

(To be a little more general to explain, if your function takes a MutableList<Animal>, you could read and write any type of Animal to that list. But you could only pass in exactly a MutableList<Animal>. If you changed it to MutableList<out Animal>, we could now pass in a MutableList<Dog> or MutableList<Cat> because you re guaranteeing you re only going to read items from that list - Dogs and Cats can be read as Animals. But the function has no idea what type is valid to put into that list - all it knows are Animals. Because the incoming list could only store Dogs and Cats (and their subtypes), the function using out can t be allowed to write to it.)

If the data inside BaseResp could be a val, you could use declaration-site variance to define this for all instances of BaseResp. For example:

open class BaseResp<out T>(val data:T? = null)

因此,你不需要在<代码>末的声明中进行类型的预测(out),而且你也经常在任何地方使用。 这取决于您是否真正需要<代码><>数据>/代码>,作为<代码>var;对于答复类型而言,你可能使之不可更改。

I d recommend reading Generics: in, out, where in the Kotlin docs for more detail.

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签