Java Advanced-Generics

Source: Internet
Author: User

> Generics: Generic refers to the typed type of the parameter, which is commonly used in interfaces, classes, methods
> function: Used to determine the range of parameters, in writing code in advance to check the code error
> generic declaration, which gives the class declaration, 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<? Super Number> Stack2) {

}
}


1. Parameter conversion >
Statement:
class Book<t extends number>{}
Reference:
Book<number> numbook=new book<number> ();
Book<integer> numbook2=new book<integer> ();
Explicit or implicit conversion of parameters at the same level is 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? This result produces a compilation error;
Assignment value:
f=i;? A compilation error is also generated;


2. Type parameter array >
Statement:
T[] T;
Defined:
t[] t=new int[10];//compilation error
Modify:
T[] T;
Int[] In=new int[]{10,20};
T=in ;


3. Type parameters static use > Since type parameters are determined only when the type is instantiated,
As the following code, the type parameter as a static variable will cause a compilation error;
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.