龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 软件开发 > JAVA开发 >

Java语言的局部类

时间:2009-12-23 15:42来源:未知 作者:admin 点击:
分享到:
假如你有一个 Integer 对象的列表,并且你想使用 Coolections.sort 来对它们进行排序。另外,你还要自己指定一个比较器,因为你想按降序而不是升序排列它们。这里有一些代码示例说明了

假如你有一个 Integer 对象的列表,并且你想使用 Coolections.sort 来对它们进行排序。另外,你还要自己指定一个比较器,因为你想按降序而不是升序排列它们。这里有一些代码示例说明了该怎么做:
  
  import Java.util.*;
  
  public class LocalDemo1 {
  
  // 使用实现了 Comparator 的匿名类排序。
  
  static void sortanon(List list) {
  
  Collections.sort(list, new Comparator() {
  
  public int compare(
  
  Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }
  
  });
  
  }
  
  // 使用实现了 Comparator 的局部类排序
  
  static void sortlocal(List list) {class MyComparator implements Comparator {
  
  public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }};
  
  Collections.sort(list, new MyComparator());
  
  }
  
  public static void main(String[] args) {
  
  List list1 = new ArrayList();
  
  list1.add(new Integer(1));
  
  list1.add(new Integer(2));
  
  list1.add(new Integer(3));
  
  sortanon(list1);
  
  System.out.println(list1);
  
  List list2 = new ArrayList();
  
  list2.add(new Integer(1));
  
  list2.add(new Integer(2));
  
  list2.add(new Integer(3));
  
  sortlocal(list2);
  
  System.out.println(list2);
  
  }}
  
  这段程序的输出如下:
  
  [3, 2, 1]
  
  [3, 2, 1]
  
  上列中使用两种不同的方法实现了 Comparator 接口。第一种方法使用匿名类,第二种方法使用局部类,二者有何区别:一点区别是格式上的??匿名类的定义比较简捷,它实际上是下面这个表达式的一部分:
  
  Comparator c = new Comparator() {...};
  
  与之相反,局部类的定义看起来非常类似于常规的类定义,略为烦琐。例如,定义局部类内时可能用到 “implements”语句,而在匿名类中不需要显示的使用这条语句。
  
  哪一种格式“更好”取决于你自己的观点。匿名类的定义会比较难读,但在不需要使用局部类的地方使用局部类会造成一些错觉,让人觉得需要做的事比实际要做的事更多。
  
  让我们来看看另一个例子,更深层的比较匿名类和局部类:
  
  import java.util.*;
  
  public class LocalDemo2 {
  
  // 使用两个单独的匿名类实例对两个列表进行排序
  
  static void sort1(List list1, List list2) {
  
  Collections.sort(list1, new Comparator() {
  
  public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);
  
  return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }
  
  });
  
  Collections.sort(list2, new Comparator() {public int compare(
  
  Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }});
  
  }
  
  // 使用一个局部类的两个实例来对两个列表进行排序
  
  static void sort2(List list1, List list2) {
  
  class MyComparator implements Comparator {
  
  public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }}
  
  Collections.sort(list1, new MyComparator());
  
  Collections.sort(list2, new MyComparator());
  
  }
  
  // 使用一个匿名类的一个实例来对两个列表进行排序
  
  static void sort3(List list1, List list2) {Comparator cmp = new Comparator() {
  
  public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }};
  
  Collections.sort(list1, cmp);
  
  Collections.sort(list2, cmp);
  
  }
  
  // 使用一个局部类的一个实例来对两个列表进行排序
  
  static void sort4(List list1, List list2) {class MyComparator implements Comparator {
  
  public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }}
  
  Comparator cmp = new MyComparator();
  
  Collections.sort(list1, cmp);Collections.sort(list2, cmp);
  
  }
  
  static class AppComparator implements
  
  Comparator {public int compare(Object o1, Object o2) {int cc = ((Integer)o1).compareTo(o2);return (cc < 0 ? 1 : cc > 0 ? -1 : 0);
  
  }}
  
  static Comparator appcomparator = new AppComparator();
  
  // 使用应用程序中定义的比较器来对两个列表进行排序
  
  static void sort5(List list1, List list2) {Collections.sort(list1, appcomparator);
  
  Collections.sort(list2, appcomparator);
  
  }
  
  public static void main(String[] args) {
  
  List list1 = new ArrayList();
  
  list1.add(new Integer(1));
  
  list1.add(new Integer(2));
  
  list1.add(new Integer(3));
  
  List list2 = new ArrayList();
  
  list2.add(new Integer(4));
  
  list2.add(new Integer(5));
  
  list2.add(new Integer(6));
  
  //sort1(list1, list2);
  
  //sort2(list1, list2);
  
  //sort3(list1, list2);
  
  //sort4(list1, list2);


  

精彩图集

赞助商链接