import java.util.Arrays; import java.util.Comparator; public class AnnInnerClassDemo { public static void main(String[] args) { Xoo xoo = new Xoo() { public int getA() { return 2; } };// 匿名类, 匿名内部类 System.out.println(xoo); // "2" Yoo yoo = new Yoo() { public int getNum() { return 88; } }; System.out.println(yoo.getNum());// 88 CharSequence cs = new CharSequence() { public int length() { return 10; } public char charAt(int index) { return 'A'; } public CharSequence subSequence(int start, int end) { return null; } }; StringBuilder str = new StringBuilder(cs); System.out.println(str); String[] strs = { "abcff", "abc", "acde", "a" }; Arrays.sort(strs, new Comparator() { public int compare(Object o1, Object o2) { String s1 = (String) o1; String s2 = (String) o2; return s1.length() - s2.length(); } }); System.out.println(Arrays.toString(strs)); } } interface Yoo { int getNum(); } class Xoo { public int getA() { return 1; } public String toString() { return Integer.toString(getA()); } }