Java Generics - What is this syntax for?

后端 未结 3 2002
误落风尘
误落风尘 2020-12-07 00:02

What does this part of the code below mean? I don\'t even know what this syntax is even called.

private class Downl         


        
相关标签:
3条回答
  • 2020-12-07 00:11

    AsyncTask is a generic class. You should look at the generics tutorial to understand the syntax and semantics of generics.

    If you look at the AsyncTask docs you will see what each of those parameters means.

    • The first is marked as "params" and is the type that your doInBackground method accepts.
    • The second is the type that is used to denote progress, as taken in the onProgressUpdate method.
    • The third is the resulting type of the task, the type that is returned from doInBackground and received by onPostExecute.
    0 讨论(0)
  • 2020-12-07 00:13
    AsyncTask<String, Void, Bitmap>
    

    Tells AsyncTask is described by 3 distinct types, String as first parameter, Void as second parameter and Bitmap as third parameter, when you use AsyncTask.

    This is called Generics in java, introduced from Java5 onwards. Please read this tutorial to understand more about Generics. Here is javadoc on how android AsyncTasktask uses generics.

    Update: From AsyncTask javadoc

    1) Params, the type of the parameters sent to the task upon execution.
    2) Progress, the type of the progress units published during the background computation.
    3) Result, the type of the result of the background computation.
    
    0 讨论(0)
  • 2020-12-07 00:32

    It's called Generics. Here is the details from the AsyncTask manual:

    The three types used by an asynchronous task are the following:

    Params, the type of the parameters sent to the task upon execution.

    Progress, the type of the progress units published during the background computation.

    Result, the type of the result of the background computation. Not all types are always used by an asynchronous task.

    To mark a type as unused, simply use the type Void:

    So AsyncTask<String, Void, Bitmap> means that, AsyncTask --DownloadImageTask accepts the param as String, Progress type is unused and returns the result as Bitmap

    0 讨论(0)
提交回复
热议问题