First look at the example:
ImportJava.util.*; classFruit { PublicString toString () {return"Fruit"; } } classAppleextendsFruit { PublicString toString () {return"Apple"; } } classperson { PublicString toString () {return"Person"; } } classclassname<t> {//main class, file name Classname.java voidshow_1 (T t) {SYSTEM.OUT.PRINTLN ("Show_1" +t.tostring ()); } <E>voidshow_2 (e e) {System.out.println ("Show_2" +e.tostring ()); } <T>voidshow_3 (T t) {SYSTEM.OUT.PRINTLN ("Show_3" +t.tostring ()); } Public Static voidMain (string[] args) {ClassName<Fruit> o =NewClassname<fruit>(); Fruit F=NewFruit (); Apple a=NewApple (); Person P=NewPerson (); System.out.println ("Show_1 Demo ________________________"); O.show_1 (f); O.show_1 (a);//o.show_1 (P); This line of code is not compiled through. Because in//Classname<fruit> has limited the global T is Fruit, so can not join person;System.out.println ("Show_2 demo ________________________"); O.show_2 (f); O.show_2 (a); O.show_2 (P); System.out.println ("Show_3 Demo ________________________"); O.show_3 (f); O.show_3 (a); O.show_3 (P); }}
Program output:
Show_1 demo ________________________show_1 fruitshow_1 appleshow_2 demo ________________________show_2 Fruitshow_2 appleshow_2 personshow_3 demo ________________________show_3 fruitshow_3 Appleshow_3 Person
The Show_2 and Show_3 methods are actually completely equivalent. This means that once T is specified as fruit in classname<t>, then Show_1 has no prefix <T>, the method can only be show_1 (Fruit object)
If there is a prefix <T> or <E>, then it is telling the compiler: This is a new specified type, with the Classname<t> class object in the T is not a half-dime relationship. That is, the show_3 in T and Show_2 in the E is an effect, that is, can be show_3 the same degree to understand as <E> void Show_3 (e) {~~~~~}
Excerpt from: What does the <T> representative in front of the <T> list<t> in generics mean? Why to add <t>?
<T> list<t> front <T> meaning