Generic usage of java learning notes

Source: Internet
Author: User

Generic: a new feature in jdk1.5 and later versions. It is used to solve security problems and is a class security mechanism.

Generic: a new feature in jdk1.5 and later versions. It is used to solve security problems and is a class security mechanism.
Benefits:
1. Transfer classCastException to the compilation period to help programmers solve the problem. Reduce running issues and ensure security.
2. Avoid the trouble of forced conversion.
Generic Format: You can <> define the reference type for the operation.
Usage:
It is usually common in the definition framework. If you see <>, You need to define the type. <> it is used to modify the type.
When the type is used, pass the data type to be stored in the set as a parameter to <>.

Generic class: defined on the class
When the referenced data types to be operated in the class are uncertain, objects are defined to complete the extension. Now generic classes are defined to complete the extension.

 

The Code is as follows: Copy code

Package com. day14.wd;
// When no generics are available in the early stage, you can extract methods and define them.
Class Teacher {
}
Class worker {

}
Class Tool {
Private Object obj;
Public void setObject (Object obj ){

This. obj = obj;
}
Public Object getObject (){
Return obj;
}
}
// After generics:
Class Utils <QQ> {
Private QQ q;
Public void setObject (QQ q ){
This. q = q;
}
Public QQ getObject (){
Return q;
}

}
Public class GenericDemo {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Tool tt = new Tool ();
Tt. setObject (new worker ());
Worker w = (worker) tt. getObject ();


Utils <Teacher> tc = new Utils <Teacher> ();
Tc. setObject (new Teacher (); // sets the object
Teacher tr = tc. getObject (); // get the object
}

}/*
The generic type defined by the generic class is valid throughout the class. After a generic class Object specifies the specific type of operation, the types of all operations are fixed.
To allow different methods to operate on different types, and the types are unknown, you can define generics on methods.
*/


Package com. day14.wd;

Class Util {
Public <T> void show (T t ){
System. out. println ("show:" + t );

};
Public <T> void print (T t ){
System. out. println ("show:" + t );

};
}
Public class GenericDemo2 {

/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Util dd = new Util ();
Dd. show (new String ("I want to succeed "));
Dd. show (new Integer (232 ));
Dd. print (new String ("I must succeed "));

}

}//

Special: static methods cannot be generic defined on the generic class. If the data type of the application operated by the static method is unknown, you can define the generic type on the method. Defined in

Public static <T> void print (T t ){};

-------------------? Wildcard -------------------
? Wildcard, which can be understood as a placeholder
? Extends E: it can accept the subtype of E or E.
? Super E: can accept the parent class of E type or E, the lower limit

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.