Generics in Java

Source: Internet
Author: User

Generics in 1.java are similar to templates in C + +, and the main purpose is to avoid repeating code and implementing code reuse.

For example Maxmin functions, Maxmin (Int,int), Maxmin (Long,long), Maxmin (double,double), because of different parameters, you may need to write three maxmin respectively.

Using generics after public <t>t maxmin (t,t), write one on the line.

Generics in 2.java can only be applied to objects, basic types such as Int,long cannot be used, and must use their object type Integer,long.

Generics in 3.java are primarily subclasses of objects, and object variables can refer to other subclasses, so generics can be considered as follows

Public Object maxmin (object,object)

Generics in 4.java only retain one copy of the code after compilation.

While the C + + template Although we only wrote a template code, but because there are several different parameters of the call, the corresponding compilation

Several corresponding codes, such as Template<class t> T maxmin (t,t), will be generated. If Maxmin (Int,int) and Maxmin (Long,long) are called in the software

Two copies of the code are generated after compilation.

Combined with code Description:

Public class test1{
public static void Main (string[] args) {
Test1 atest1=new Test1 ();
DISP1 (123.456);//error
Atest1.disp1 (123.456);
DISP2 (345);

gclass1<string> agclass1=new gclass1<string> ("ABFSF");
Agclass1.display ();

gclass1<long> bgclass1=new gclass1<long> (12345l);
Bgclass1.display ();

Gclass2 agclass2=new Gclass2 ("What a Fuck object");
Agclass2.display ();

Gclass2 bgclass2=new Gclass2 (123.456);
Bgclass2.display ();

Gclass4 agclass4=new Gclass4 ("This is Gclass4");
Gclass3<gclass4> agclass3=new gclass3<gclass4> (AGCLASS4);
Agclass3.display ();

EGclass4 aegclass4=new EGclass4 ("This is EGclass4");
Gclass3<egclass4> bgclass3=new gclass3<egclass4> (AEGCLASS4);
Bgclass3.display ();

}

Public <fuck>fuck Disp1 (Fuck t) {
Fuck bt=null;
bt=t;
System.out.println ("This is Disp1 ():" +BT);
return BT;
}

public static void Disp2 (int temp) {
System.out.println ("This is DISP2 ():" +temp);
}

}

Class gclass1<t>{
Gclass1 (T t) {this.at=t;}
public void Display () {
System.out.println ("Gclass1 disp:" +this.at);
}
Private T At=null;
}

Class gclass2{
Gclass2 (Object t) {this.at=t;}
public void Display () {
System.out.println ("Gclass2 disp:" +this.at);
}
Private Object At=null;
}

Class Gclass3<t extends gclass4>{
GCLASS3 (T t) {this.at=t;}
public void Display () {
System.out.println ("GGclass3 disp ():" +this.at.getclass ());
This.aT.Display ();
}
Private T At=null;
}

Class gclass4<t>{
GCLASS4 (T t) {this.at=t;}
public void Display () {
System.out.println ("GGclass4 disp ():" +this.at);
}
Public T At=null;
}

Class EGclass4 <T> extends gclass4<t>{
EGCLASS4 (T t) {
Super (T);
}
public void Display () {
System.out.println ("EGclass4 disp ():" +super.at);
}
}

/////////////////////////////////////////////////

Output results

This is DISP1 (): 123.456
This is DISP2 (): 345
Gclass1 DISP:ABFSF
Gclass1 disp:12345
Gclass2 disp:what a Fuck object
Gclass2 disp:123.456
GGclass3 disp (): Class Gclass4
GGclass4 disp (): This is GCLASS4
GGclass3 disp (): Class EGclass4
EGclass4 disp (): This is EGCLASS4

//////////////////////////////////////////////////////////////////////////////////////////

The first is a public class Test1, the static function main calls a non-static static generic function through the object ATest1 DISP1 (if not through the object, directly call non-static illegal)

Then the static function Disp2 is called directly;

Created a generic class gclass1,gclass2. Where Gclass2 is created by object, as with the Gclass1 effect.

Created a generic class Gclass4, her subclass generic class EGclass4

A generic class GCLASS3 is created that restricts the parameter type to GCLASS4 or its subclasses through the <t extends gclass4>.

Because of the qualification, you can use GCLASS3 to declare variables only with GCLASS4 or its subclasses

gclass3<Gclass4> Agclass3=new gclass3<gclass4> (AGCLASS4);

gclass3<EGclass4> Agclass3=new gclass3<egclass4> (AEGCLASS4);

Generics in Java

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.