Think in Java (12): Generics

Source: Internet
Author: User

1. Using a generic method can replace the entire class of generic words, to define a generic method, simply place the generic parameter list before the return value:
public class Genericmethods {public <T> void F (T x) {System.out.println (X.getclass (). GetName ());} public static void Main (string[] args) {genericmethods gm = new Genericmethods (); GM.F (""); Gm.f (1); GM.F (1.0); gm.f (1.0F); GM.F (' C '); GM.F (GM);}}

2. Variable parameters can coexist well with generic methods:
public class Genericvarargs {public static <T> list<t> makeList (T ... args) {list<t> result = new Arrayli St<t> (); for (T Item:args) Result.add (item); return result;} public static void Main (string[] args) {list<string> ls = makeList ("A"); SYSTEM.OUT.PRINTLN (ls); ls = makeList ("A", "B", "C"); SYSTEM.OUT.PRINTLN (ls), ls = makeList ("abcdeffhijklmnopqrstuvwxyz". Split ("")); SYSTEM.OUT.PRINTLN (LS);}}
3. The boundary allows you to set restrictions on the type of parameters used for generics.
Class Aaa<t extends Bbb>

4.<? Extends t> is not a collection, but the meaning of a certain seed class of T, remember is one kind, the question comes, because even which kind is uncertain, bring the uncertainty, so it is impossible to pass
Add () to add an element. You might also think why not add (T)? Because of < Extends t> is a seed class of T, a container that can be placed in a subclass may not be placed in a superclass, that is, it is not possible to put T
public class Genericsandcovariance {public static void main (string[] args) {list<? extends fruit> flist = new Arrayl Ist<apple> ();//Compile Error:can ' t add any type of object://Flist.add (New Apple ());//Flist.add (New Fruit ());//F List.add (New Object ()); Flist.add (null); Legal but uninteresting//We know the It returns at least fruit:fruit f = flist.get (0);}}
5.<: Super t> Here is easier to use, not <? extends t> so many restrictions, here means that the T class is the lower bound of a certain kind,
In short, it is the super class of T class. But why is add (T) possible? Because a container can be put into a certain class can be put into its sub-class, polymorphic concept.

Think in Java (12): Generics

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.