This first knowledge combing blog, I hope I can continue to summarize the knowledge.
This is the video you see in the Geek College, the catalogue is as follows, pretty clear. Then I'll remember what I said in the video.
Java object-oriented generics: Understanding Generics
A new feature of Java SE 1.5, the essence of generics is a parameterized type that can represent any data type.
Java object-oriented generics: using in constructors
Class gen<t>{
private T value;
No angle brackets are required for generics in Gen (T value) {//constructor
This.value=value;
}
public void SetValue (T t) {
this.value=t;
}
Public T GetValue () {
return this.value;
}
}
Public Class Demo () {
public static void Main (string[] args) {
gen<string> g = new gen<string> ("test");
Output G.getvalue ();
}
}
Java object-oriented generics: Specify multiple generics (typically two)
Class gen<t,k>{
Private T take;
Private K key;
Gen (T take,k key) {///constructor does not need angle brackets for generics
This.take=take;
This.key=key;
}
... set get method
}
Public Class Demo () {
public static void Main (string[] args) {
gen<string,integer> g = new gen<string,integer> ();
G.settake ("test");
G.setkey (10);
}
}
Java object-oriented generics: wildcard characters
When you want to specify a generic type for a parameter in a method, use the "? "For example, because the parameter is info<t>
Java object-oriented generics: generic interface
Add <T> after interface name
Interface gen<t>{
public void say ();
}
1. Class Genimpl implements gen<string>{
Public Void Say () {
}
}
2.Class genimpl<t> implements gen<t>{
Public Void Say () {
}
}
Specifying the data type when using Genimpl specifically
Java object-oriented generics: generic methods
Public <T> T method name (T t)
Returns the type of the T type assigned to the call, int i = tell (10);
Java object-oriented generics: generic arrays
public static <t>void Tell (T arr[]) {
for (int i=0;i<arr.length;i++) {
System.out.print (Arr[i])
}
}
Integer arr[]={1,2,3,4};
Tell (ARR);
http://blog.csdn.net/flfna/article/details/6576394
Java Object-oriented generics