java-using a generic shape

Source: Internet
Author: User

1. Classes, methods on the generics

Generic class-defined generic, valid throughout the class. If it is used by the method, the type of the generic class is fixed after the specific type of operation is explicitly made. To allow different methods to manipulate different types, the types are not yet deterministic. You can then define the generic on the method. /* Special: Static methods do not have access to generics defined on the class. If the application data type for a static method operation is not deterministic, you can define the generic on the method. */class demo<t>{public  void Show (T t) {System.out.println ("show:" +t); Public <Q> void print (q q) {System.out.println ("Print:" +q);} Public  static <W> void method (W t) {System.out.println ("method:" +t);}}

2. Interface to achieve the interface of the generic

/generics are defined on the interface. Interface inter<t>{void Show (T t);} /*class Interimpl implements Inter<string>{public void Show (String t) {System.out.println ("show:" +t);}} */class interimpl<t> implements inter<t>{public void Show (T t) {System.out.println ("show:" +t);}}

3. A generic qualifier, wildcard ( Note:? Wildcard characters can only be used on a class that has a defined generic , making a generic qualification on that class )

/*? A wildcard character. Can also be understood as placeholders. The qualification of generic type;? Extends e: Can receive subtypes of type E or E. Ceiling.? Super E: Can receive the Type E or the parent of E. Lower limit note:? Wildcards can only be used on classes that already have a generic defined, the generic qualification on that class */class  genericdemo6{public static void Printcoll (COLLECTION<? extends person > al) {iterator<? extends person> it = Al.iterator (); while (It.hasnext ()) {System.out.println (It.next (). GetName ());}} public static void Printcoll (Arraylist<?> al)//arraylist al = new arraylist<integer> (); error{iterator<? > it = Al.iterator (); while (It.hasnext ()) {System.out.println (It.next (). toString ());}} }

Class fu<t>
{

}
Class Interimp
{


public void Integer (FU<? extends integer> f) {


}
public void string (FU<? Super String> F)
{

}

}

  

java-using a generic shape

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.