Why & lt; T & gt;

Source: Internet
Author: User

Fruit {String toString () {"Fruit" Apple Fruit {String toString () {"Apple" Person {String toString () {"Person" ClassName <T> {"show_1" + <E> "show_2" + <T> "show_3" + <Fruit> o = ClassName <Fruit >==" show_1 demonstrates ____________________ "System. out. println ("show_2 demo ____________________" "show_3 demo ________________________"

Output:

Show_1 demo ________________________
Show_1 Fruit
Show_1 Apple
Show_2 demonstration ________________________
Show_2 Fruit
Show_2 Apple
Show_2 Person
Show_3 demonstration ________________________
Show_3 Fruit
Show_3 Apple
Show_3 Person

/* The show_2 and show_3 methods are completely equivalent. That is to say, if show_1 has no prefix <T> after T is specified as Fruit, this method can only be show_1 (Fruit object)

If you have a prefix <T> or <E>, you will tell the compiler to say: this is a new type I specified,
It has no relationship with T in the Class Object of ClassName <T>. In other words, T and show_2 in show_3
E is an effect, that is, you can geographically resolve show_3 to <E> void show_3 (E e ){~~~~~}

From what I said above ,*/

<T> you can understand it as a type declaration. Otherwise, a "T" suddenly appears in your return value and function parameters. The Compiler knows what this is and will certainly report an error, therefore, we need to understand this issue from the compiler perspective.
Just like int a. The variable a can be used later.

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.