Java in the first talk of generics

Source: Internet
Author: User
Tags uppercase letter

/*
*
* Generics and C + + templates have similar ideas
* Generics are object-oriented
*
* Function Basic use format <T> return value function name () {}//can be any uppercase letter is not necessarily a T
* Class Basic use Format class Classname<t> {} and Class Classname<t extend interface & implements>{}
*
* used in the same way as the normal class is used in the form of new only t to have specific classes (not objects) instead

Generic non-call generic methods and generic classes are tested under common conditions (all of which understand that wildcards are not described)


*/
public class Jgnenerics {
static public <g > void Test (G []t) {
for (G m:t) {
System.out.println (m);
}
}
Compare three values and return a maximum value
public static <t extends comparable<t>> t maximum (t X, t y, T Z)
{
T max = x; Assuming X is the initial maximum value
if (Y.compareto (max) > 0) {
max = y; Greater Y
}
if (Z.compareto (max) > 0) {
max = Z; Now Z is bigger.
}
return Max; Returns the largest object
}
public static void Main (String argc[]) {
Integer i[]={10,100,1000};
Test (i);
Maximum (1,2,1);

R<m1> r=new r<m1> ();//This look is perfectly possible.
Mygenerics}
}
Interface generics<g>{
public void Test (G t);
}

Class Mygenerics<t extends generics>{//note there is no <> in the back of this thing or it will be reported type mismatch

}
Class H<string > Implements generics<string>{
public void Test (String a) {}

}


Class m{}
Class M1 extends m{}
Class R<g extends m>{}

Java in the first talk of 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.