結果:
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