一、定義一個簡單的“泛型”類
package tigers;
public class GenericIdentify {
public static void main(String[] args) {
Generic1 gStr = new Generic1();
gStr.set("aaaaaaaaaaaaaaaaa");
Generic1 gInt = new Generic1();
gInt.set(new Integer(1321423));
Generic1 gBool = new Generic1();
gBool.set(new Boolean(true));
System.out.println(gStr.get() + "\n" +
gInt.get() + "\n" +
gBool.get() + "\n");
((Generic1)gInt).set("bbbbbbbbbbbbb"); //避開類型檢查
System.out.println(gInt.get());
}
}
class Generic1 { //聲明泛型
private A a;
public void set(A a) {
this.a = a;
}
public A get() {
return this.a;
}
}
C:\tiger>javac -d c:\tiger\cls\ c:\tiger\src\*.java
Note: c:\tiger\src\GenericIdentify.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\tiger>java -classpath c:\tiger\cls tigers.GenericIdentify
aaaaaaaaaaaaaaaaa
1321423
true
bbbbbbbbbbbbb
C:\tiger>
二、關於格式化輸出的簡單例子
package tigers;
import java.util.Formatter;
public class FormatPrint2 {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder();
Formatter forma = new Formatter(sb);
forma.format("%12.10f", Math.PI);
System.out.println(forma);
System.out.printf("%12.10f", Math.PI);
}
}
結果:
C:\tiger>javac -d c:\tiger\cls c:\tiger\src\FormatPrint2.java
C:\tiger>java -classpath c:\tiger\cls tigers.FormatPrint2
3.1415926536
3.1415926536
三、可變長度參數與格式化輸出
package tigers;
public class FormatPrint {
public static void main(String[] args) {
printf("%, %, % and % are my friends!", "BITAN", "HELEN", "KATHY", "SUSAN");
printf("%, %, %, % and % are my favorites" ,"A", "B", "C", "D");
}
public static void printf(String msg, String... strs) {
int i = 0;
char[] chars = msg.toCharArray();
for ( char c : chars) {
System.out.print(c == '%' ? strs[i++] : c);
}
System.out.println();
}
}
C:\tiger>javac -d c:\tiger\cls\ c:\tiger\src\*.java
C:\tiger>java -classpath c:\tiger\cls tigers.FormatPrint
BITAN, HELEN, KATHY and SUSAN are my friends!
A, B, C, D and Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
at tigers.FormatPrint.printf(FormatPrint.java:12)
at tigers.FormatPrint.main(FormatPrint.java:6)
四、使用‘泛型’與‘增強for循環’的簡單例子
genericTest.java
package tigers;
import java.util.*;
public class genericTest {
public static void main(String[] args) {
List<String> lists = new ArrayList<String>();
lists.add("aaaaaaaaaaaa");
lists.add("bbbbbbbbbbbbb");
lists.add("cccccccccccccc");
lists.add("ddddddddddddd");
lists.add("eeeeeeeeeeeeeeeee");
for (String s :lists) {
System.out.println(s);
}
}
}
結果:
C:\forms>javac -d c:\forms\web-inf\classes
c:\forms\src\genericTest.java
C:\forms>java -classpath c:\forms\web-inf\classes tigers.genericTest
aaaaaaaaaaaa
bbbbbbbbbbbbb
cccccccccccccc
ddddddddddddd
eeeeeeeeeeeeeeeee
C:\forms>
五、使用‘枚舉’類型
package tigers;
import java.util.EnumMap;
enum Simple1 {
A,B,C,D;
}
public class SimpleEnum {
public static void main(String[] args) {
System.out.println(Simple1.A);
for (Simple1 s : Simple1.values()) {
System.out.print(s);
}
System.out.println();
Simple1 s2 = Simple1.D;
switch(s2) {
case A:
System.out.print("A");
break;
case B:
System.out.print("B");
break;
case C:
System.out.print("C");
break;
default:
System.out.print(s2);
}
System.out.println();
EnumMap<Simple1, String> aMap = new EnumMap<Simple1, String>(Simple1.class);
String str = "something";
int i = 0;
for (Simple1 s3 : Simple1.values()) {
aMap.put(s3, str += (i++ + " "));
}
System.out.println(aMap);
for (Simple1 s4 : Simple1.values()) {
System.out.println(s4 + " = " + aMap.get(s4));
}
}
}
結果:
C:\tiger>javac -d c:\tiger\cls c:\tiger\src\SimpleEnum.java
C:\tiger>java -classpath c:\tiger\cls tigers.SimpleEnum
A
ABCD
D
{A=something0 , B=something0 1 , C=something0 1 2 , D=something0 1 2 3 }
A = something0
B = something0 1
C = something0 1 2
D = something0 1 2 3
六、在類定義上使用泛型和繼承
package tigers;
import java.io.UnsupportedEncodingException;
public class GenericExtends {
public static void main(String[] args) {
try {
ActionToucher.execute(
new Action<NoSuchMethodException>() {
public void method() throws NoSuchMethodException {
System.out.println("Action<NoSuchMethodException>");
}
}
);
ActionToucher.execute(
new Action<NoSuchFieldException>() {
public void method() throws NoSuchFieldException {
System.out.println("Action<NoSuchFieldException>");
}
}
);
ActionToucher.execute(
new Action<UnsupportedEncodingException>() {
public void method() throws UnsupportedEncodingException {
System.out.println("Action<UnsupportedEncodingException>");
}
}
);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
interface Action<E extends Exception> {
void method() throws E;
}
class ActionToucher {
public static <E extends Exception> void execute(Action<E> action) throws E {
action.method();
}
}
C:\tiger>javac -d c:\tiger\cls\ c:\tiger\src\*.java
Note: c:\tiger\src\GenericIdentify.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
C:\tiger>java -classpath c:\tiger\cls tigers.GenericExtends
Action
Action
Action