Java Advanced-Generics

Source: Internet
Author: User

> generics: Generic refers to the typed type of the parameter, which is commonly used in interfaces. Class. Method in
> function: Used to determine the range of the parameters, in the writing code in advance to check the code error
> Generic declarations, the class declarations are given below, and so on:
class classname<t>{}
Class classname<k,v,o>{}



> General character:<?> General designation of all classes
> Qualifier: <t exteds Super Class >
1) Qualifier letters can only be limited once
2) When extends can only use capital letters, when super can only use "?"
3) You cannot use the Super qualifier symbol in a class.
4) The qualifier in the class can be used by the method.
5) When the interface is declared with a qualified symbol, its subclasses must also have.


Class Book<t extends number>{

void Go (stack<t> Stack) {


}


public void Go2 (STACK&LT;?

Super Number> Stack2) {

}
}


1. Conversion of parameters >
Statement:
class Book<t extends number>{}
Reference:
book<number> numbook=new book<number> ();
Book<integer> numbook2=new book<integer> ();
Explicit or implicit conversions at the same level are wrong
numbook= (book<number>) numBook2;

2. Compare and assign values:
Statement:
class compareclass<t>{
T A;
Compareclass (T t) {
this.a=t;
}
}

Comparison:
Compareclass<float> f=new compareclass<float> ();
Compareclass<integer> i=new compareclass<integer> ();

F==i? Such a result would produce a compilation error;
Assignment value:
f=i;? The same will also generate compilation errors.


2. Array of type parameters >
Statement:
T[] T;
Defined:
t[] t=new int[10];//compilation error
Changes:
T[] t;
Int[] In=new int[]{10,20};
T=in ;



3. Type parameters static use > because type parameters only determine the type when instantiated, so
For example, the following code. The use of type parameters as static variables will cause compilation errors;
class person<k,v>{

static K name;
Static V age;
Static V GetName () {
return age;
}

}



Java advanced-generics

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.