Java Basics Summary Generic

Source: Internet
Author: User

Generics implement the concept of parameterized types so that code can be applied to multiple types.

1. Generic class

A declared generic type static method cannot be used

class Tools<t>{    private  T t;      Public void set (T t) {        this. T = t;    }       Public t get () {        returnthis. T;    }}
2. Generic methods
classGenerictest2<t>{    //using generics defined by a class     Public voidprint1 (T t) {SYSTEM.OUT.PRINTLN ("Print1:" +t); }    //generics defined using the method     Public<E>voidPrint2 (e e) {System.out.println ("Print2:" +e); }    //static methods cannot use generic types defined by a class     Public Static<E>voidPrint3 (e e) {System.out.println ("Print3:" +e); }}
3. Generic interface
//generic interfaceInterfaceInter<t>{    voidShow (T t);}//classInterImpl2ImplementsInter<string>{     Public voidShow (String t) {System.out.println ("Show:" +t); }    }//classInterimpl<t>ImplementsInter<t>{     Public voidShow (T t) {SYSTEM.OUT.PRINTLN ("Show:" +t); }}classgenericdemo5{ Public Static voidMain (string[] args) {Interimpl<Integer> i =NewInterimpl<integer>(); I.show (5); //Interimpl i = new Interimpl (); //i.show ("Liu");     }}
4. Generic Qualification
extends New extends Person> ();   // restrict the objects that store the person or person child class Super New Super Person> ();   // restrict the objects that store the person or person parent class

Java Basics Summary Generic

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.