<T> list<t> front <T> meaning

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.