Java generics (1)

Source: Internet
Author: User

Generic classification

1. generic classes and interfaces

2. Generic Method

Package generic; import Java. util. arraylist; import Java. util. list;/*** generic class and generic Method */public class genericclass <E> {private e data; public e getdata () {return data ;} // generic method // This method can generate a list public <t> List <t> gett (class <t> CLs, int size) of any object through the class object) {list <t> List = new arraylist <t> (); try {for (INT I = 0; I <size; I ++) {// its restriction type is that the class corresponding to the class must contain the list of non-argument constructor methods. add (CLS. newinstance () ;}} catch (exception e) {e. printstacktrace ();} return list;} package generic; public interface genericinter <E> {e get ();} package generic; public class genericinterimpl implements genericinter <string >{@ override Public String get () {return NULL ;}}

 

Generic Boundary

Package generic; public class holder <E> {private e data; Public void set (E) {This. data = E;} public e get () {return data ;}} package generic; public class boundtest {// native parameter method public void rawargs (Holder, object O) {holder. set (o); // because the shape parameter holder does not specify its generic data type, the compilation generates a warning object OBJ = holder. get (); integer I = (integer) Holder. get (); // an exception may occur because you cannot know what type of data is stored in the holder. // a warning is generated during compilation.} // the unbounded Parameter Method P Ublic void unboundedargs (holder <?> Holder, object O) {// holder. Set (0); // This sentence cannot be compiled because Holder <?> Which means that holder needs to // specify the exact generalized data type. What do I think is an unbounded wildcard? The purpose of this function is to notify the // compiler that the holder must specify the exact data type, instead of using other // types to use this holder object OBJ = holder. get (); integer I = (integer) Holder. get (); // actually unique get () the unique value returned by the method can be matched to ensure that no error is made. Only the // object class is used.} // you can determine the generic public <t> void wildsubtype (holder <? Extends T> holder, t Arg) {// holder. set (ARG); // This sentence will cause compilation errors. Imagine how we can transmit parameters when we call this method, // holder <arraylist> holder = new holder <arraylist> (); // collection Arg = new hashmap (); // wildsubtype (holder, ARG ); // meaning imagine what will happen when the Set () method of holder is called after the above holder and Arg are passed in. // result, this is why the compiler does not allow t replacement when determining the lower limit of generics? Extends t //, because T may be map, and our? It is a list t = holder. Get (); // This sentence can be executed, because? Extends T, then? It must be a T type} // The generic public <t> void wildsupertype (holder <? Super T> holder, t Arg) {holder. Set (ARG); // This sentence can be executed, because? Super T, then t must be? The statement class // t = holder. Get (); // This sentence cannot be compiled because? Super T may not be T, right? Is a collection // while T is an arraylist }}

 

 

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.