Java generic programming

Source: Internet
Author: User
ArticleDirectory
    • 4) restrict wildcard and wildcard characters

1. Concept

Generics are parameterized types. The advantage of generics is to check the type security during compilation, and all the forced conversions are both automatic and implicit, improvingCode.

2. Case studies

1) Let's take a look at the following cases.

 //  Not applicable to generic programming  
Apple app0 = New Apple ();
Apple app1 = New Apple ();

List li = New Arraylist ();
Li. Add (app0 ); // No error is found when you add a non-required type.
Li. Add (app1 );

Apple appused = (Apple) Li. Get (0 );

// Generic programming is as follows:
Apple app0 = New Apple ();
Apple app1 = New Apple ();

List <Apple> li = New Arraylist <Apple> ();
Li. Add (app0 ); // If the added object type is incorrect, the compiler will find it. Specify the object type to be held by the container, and use the compiler to ensure the correctness of the type.
Li. Add (app1 );

Apple appused = Li. Get (0 );

Advantages of Using Generics: large applications can be significantly reducedProgramComplexity;GenericIt is possible for large optimization: This type of errors can be found during compilation, and no type judgment is required when elements are retrieved, thus improving the program running timeliness rate.

2) generic classes

There are two classes as follows: Construct the objects of the two classes and print their respective member X.

 Public   Class Stringfoo {

Private String X;

Public Stringfoo (string X ){
This . X = X;
}


Public String getx (){
Return X;
}

Public Void Setx (string X ){
This . X = X;
}
}

Public Class Doublefoo {

Private Double X;

Public Doublefoo (Double X ){
This . X = X;
}

Public Double getx (){
Return X;
}

Public Void Setx (Double X ){
This . X = X;
}
}

Implementation with generics

 Public   Class Genericsfoo <t> {

Private T x;

Public Genericsfoo (t x ){
This . X = X;
}

Public T getx (){
Return X;
}

Public Void Setx (t x ){
This . X = X;
}
}


Code implementation:

Public Class Genericsfoodemo {

Public Static Void Main (string ARGs []) {

Genericsfoo <string> strfoo = New Genericsfoo <string> ("Hello generics! ");
Genericsfoo <double> doufoo = New Genericsfoo <double> ( New Double ("33 "));
Genericsfoo <Object> objfoo = New Genericsfoo <Object> ( New Object ());

System. Out. println ("strfoo. getx =" + strfoo. getx ());
System. Out. println ("doufoo. getx =" + doufoo. getx ());
System. Out. println ("objfoo. getx =" + objfoo. getx ());
}
}

 

3) Generic Method

Whether a generic method exists. It has nothing to do with whether the class is generic. To define a generic method, you only need to place the list of generic parameters before the return value.

 
Public ClassExamplea {

Public<T>VoidF (t x ){
System. Out. println (X. getclass (). getname ());
}

Public Static VoidMain (string [] ARGs ){

Examplea Ea =NewExamplea ();

EA. F ("EA ");
EA. F (10 );
EA. F ('A ');
}
}

When using the generic method, you do not need to specify the parameter type. The compiler will find the specific type by itself. In addition to different definitions, generic methods call similar methods. Note that a static method cannot access the type parameters of a generic class. Therefore, to use a static method, you must make it a generic method.

 

4) restrict wildcard and wildcard characters

Class genericsfoo <t extends collection>. In this way, the generic T in the class can only be the implementation class of the collection interface, and an error occurs when the non-collection interface is passed in.

Class genericsfoo <? Extends collection>, "?" Represents the unknown type (wildcard), which is the collection interface. <? Extends type> indicates that this type is a child type of a certain type.

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.