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