首页 > 编程知识 正文

java的网络编程重要吗,java中泛型的使用

时间:2023-05-04 00:58:48 阅读:115596 作者:1403

thisisfromheadfirstjava : (page 575 )。

This:

公共语音识别(阵列列表) )。

Does the same thing as this:

publicvoidtakething (阵列扩展签名列表) )。

sohereismyquestion 3360 iftheyareexactlysame,why don't we write

publicextendsanimalvoidtakething (阵列列表) )。

公共语音识别(阵列列表) )。

Also,when would it be useful to use a? insteadofatinamethoddeclaration (as above ) with Generics,or for a Class declaration? What are the benefits?

The big difference between

公共语音识别(阵列列表) )。

与与

publicvoidtakething (阵列扩展签名列表) )。

isthatintheformermethodyoucanreferto ' t ' withemethodastheconcreteclassthatwasgiven.inthesecondmethodyoucannotdothis。

hereamorecomplexexampletoillustratethis :

//hereicanreturntheconcretetypethatwaspassedin

publicmapgetnamesmap{

Map names=new HashMap (;

for(tanimal3360list ) {

names.put(animal,animal.getName ); //i assume there is a getName method

}

返回名称;

}

//here i have to use general Animal

publicmapgetnamesmap (阵列扩展签名列表)。

Map names=new HashMap (;

动态动画:列表(for ) {

names.put(animal,animal.getName ); //i assume there is a getName method

}

返回名称;

}

withthefirstmethodifyoupassinanlistofcatsyougetamapwithcataskey.thesecondmethodwouldalwaysreturnamapwithgeneralanimalkey。

bythewaythisisnotvalidjavasyntax :

publicextendsanimalvoidtakething (阵列列表) )。

usingthisformofgenericmethoddeclarationyouhavetouseavalidjavaidentifierandnot '? 水平。

Edit:

The form '? 扩展类型' onlyappliestovariableorparametertypedeclaration.withinagenericmethoddeclrationithastobe ' identifierextendstypptobe

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。