[Java Study Notes] Java 6 generic instances

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

If you do not use a generic type, the data type is uncertain as follows:

Class stash {
Private object X;
Void set (Object X ){
This. x = X;
}
Object get (){
Return (X );
}
}
Public class stashone {
Public static void main (string Arg []) {
Stash stash = new stash ();
Stash. Set ("ABCD ");
String STR = (string) stash. Get ();
}
}

If the generic type is used:

Class stashstring {
Private string X;
Void set (string X ){
This. x = X;
}
String get (){
Return (X );
}
}
Public class stashtwo {
Public static void main (string Arg []) {
Stashstring stash = new stashstring ();
Stash. Set ("ABCD ");
String STR = stash. Get ();
}
}

You can also specify the type when creating an object:

Class stash <t> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stashthree {
Public static void main (string Arg []) {
Stash <string> stash = new stash <string> ();
Stash. Set ("ABCD ");
String STR = stash. Get ();
}
}

Or when assigning values:

Class stash <t> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stashfour {
Public static void main (string Arg []) {
Stash <Object> stash = new stash <Object> ();
Stash. Set ("ABCD ");
String STR = (string) stash. Get ();
}
}

To specify that a parameter is a generic type of a certain type and its sub-classes:

Class stash <t extends number> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stashfive {
Public static void main (string Arg []) {
Stash <integer> istash = new stash <integer> ();
Integer ten = new INTEGER (10 );
Istash. Set (TEN );
Ten = istash. Get ();
Stash <double> dstash = new stash <double> ();
Double Pi = new double (3.14159 );
Dstash. Set (PI );
Pi = dstash. Get ();
}
}

For interfaces:

Import java. util. eventlistener;
Import javax. Swing. jtable;
Import javax. Swing. Undo. undomanager;
Class stash <t extends eventlistener> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stashsix {
Public static void main (string Arg []) {
Stash <jtable> tablestash = new stash <jtable> ();
Jtable table = new jtable ();
Tablestash. Set (table );
Table = tablestash. Get ();
Stash <undomanager> dstash = new stash <undomanager> ();
Undomanager unman = new undomanager ();
Dstash. Set (unman );
Unman = dstash. Get ();
}
}

If you want to define both the class and the implementation of an interface, then:

Import java. AWT. image;
Import java. AWT. image. bufferedimage;
Import java. AWT. Transparency;
Class stash <t extends Image & transparency> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stashseven {
Public static void main (string Arg []) {
Stash <bufferedimage> bufstash = new stash <bufferedimage> ();
Bufferedimage bufimage = new bufferedimage (50, 50, 0 );
Bufstash. Set (bufimage );
Bufimage = bufstash. Get ();
}
}

Wildcard generic classes allow you to perform fuzzy processing when creating generic class pointers:

Class stash <t> {
Private t x;
Void set (t x ){
This. x = X;
}
T get (){
Return (X );
}
}
Public class stasheight {
Public static void main (string Arg []) {
Stash <? Extends number> numberstash;
Stash <integer> integerstash;
Integerstash = new stash <integer> ();
Integerstash. Set (New INTEGER (10 ));
Numberstash = integerstash;
Number = numberstash. Get ();
System. Out. println (number. tostring ());
Stash <double> doublestash;
Doublestash = new stash <double> ();
Doublestash. Set (New Double (3.14159 ));
Numberstash = doublestash;
Double dnumber = (double) numberstash. Get ();
System. Out. println (dnumber. tostring ());
}
}

Generics can also be nested:

Class pair <t, u> {
Private T left;
Private u right;
Pair (t l, u r ){
Left = L;
Right = R;
}
Public t getleft (){
Return (left );
}
Public U getright (){
Return (right );
}
}
Class stash <t> {
Private t;
Void set (t ){
This. t = T;
}
T get (){
Return (t );
}
}
Public class nesting {
@ Suppresswarnings ("unchecked ")
Public static void main (string Arg []) {
Stash <pair <string, long> sp;
SP = new stash <pair <string, long> ();
Pair pair = new pair ("average", new long (320 ));
Sp. Set (pair );
Pair = sp. Get ();
System. Out. println (pair. getleft () + "" + pair. getright ());
}
}

In addition, generics are not just classes, but methods can also be generic:

Import java. AWT. color;
Public class genericmethod {
Public static void main (string Arg []) {
Genericmethod GM = new genericmethod ();
GM. objtype ("ABCD ");
GM. gentype ("ABCD ");
GM. objtype (color. Green );
GM. gentype (color. Green );
}
Public void objtype (Object t ){
System. Out. println (T. getclass (). getname ());
}
Public <t> void gentype (t ){
System. Out. println (T. getclass (). getname ());
}
}

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Related Article

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.