[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]
继续上节内容,还是以对各种类型的数组进行排序为例,这时,我们考虑复杂一点,如果,我们不再按照Cat的身高进行排序,我们想以Cat的年龄作为排序的依据,再如,我们不再按Dog的food进行排序,而是以Dog的体重作为依据进行排序,该怎么办呢,当然了,我们可以改动Cat类,Dog类,让其实现这些功能,但是,当类很多时,要改动每一个类,也是很麻烦的,也就是现在的这种结构不好实现。所以,我们希望我们能灵活的指定比较依据,所以,很容易想到,这要用到多态,我们可以定义一个比较器接口Comparator,然后,根据需要我们可以自定义每种类型的各种比较器如CatHeightComparator和CatWeightComparator都实现公共的比较器接口,最后,我们只需改动Cat类的compareTo()方法就行,具体实现如下:
Comparator.java
/**CatHeightComparator.java
* 比较器
* @author jesson
*
*/
public interface Comparator {
int compore(Object o1,Object o2);
}
/**CatWeightComparator.java
* Cat类型高度比较器
*
* @author jesson
*
*/
public class CatHeightComparator implements Comparator {
@Override
public int compore(Object o1, Object o2) {
// TODO Auto-generated method stub
Cat c1 = (Cat) o1;
Cat c2 = (Cat) o2;
if (c1.getHeight() > c2.getHeight())
return 1;
else if (c1.getHeight() < c2.getHeight())
return -1;
else
return 0;
}
}
/**Comparable.java
* Cat类型重量比较器
* @author jesson
*
*/
public class CatWeightComparator implements Comparator {
@Override
public int compore(Object o1, Object o2) {
// TODO Auto-generated method stub
Cat c1 = (Cat) o1;
Cat c2 = (Cat) o2;
if (c1.getWeight() > c2.getWeight())
return -1;
else if (c1.getWeight() < c2.getWeight())
return 1;
else
return 0;
}
}
/**
* 可比较接口
* 定义了一个比较方法
* @author jesson
*
*/
public interface Comparable {
public int compareTo(Object o);
}
Cat.java
/**DataSorter.java
* Cat类 有属性身高,体重及toString()方法
*
* @author jesson
*
*/
public class Cat implements Comparable {
private int height; // 身高
private int weight; // 体重
//private Comparator comparator = new CatHeightComparator(); // 高度比较器
private Comparator comparator = new CatWeightComparator(); // 重量比较器
public Comparator getComparator() {
return comparator;
}
public void setComparator(Comparator comparator) {
this.comparator = comparator;
}
public Cat(int height, int weight) {
// TODO Auto-generated constructor stub
this.height = height;
this.weight = weight;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
/**
* 重写toString()方法
*/
@Override
public String toString() {
return this.getHeight() + "|" + this.getWeight();
}
/**
* 实现Comparable接口的compareTO方法
* 调用具体的比较器进行比较大小
*/
@Override
public int compareTo(Object o) {
return comparator.compore(this, o);
}
}
/**Test.java
* 排序类
*
* @author jesson
*
*/
public class DataSorter {
/**
* 冒泡排序方法
*
* @param Cat类型数组
*/
public static void bubbleSort(Object[] a) {
for (int i = a.length - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
Comparable o1 = (Comparable)a[j];
Comparable o2 = (Comparable)a[j+1];
if (o1.compareTo(o2) == 1) {
swap(a, j, j + 1);
}
}
}
}
/**
* 冒泡排序方法
*
* @param a
* 整数数组
*/
public static void bubbleSort(int[] a) {
for (int i = a.length - 1; i >= 1; i--) {
for (int j = 0; j < i; j++) {
if (a[j] > a[j + 1]) {
swap(a, j, j + 1);
}
}
}
}
/**
* 交换两个数据
*
* @param a
* Object类型数组
* @param x
* 数组下标1
* @param y
* 数组下标2
*/
private static void swap(Object[] a, int x, int y) {
// TODO Auto-generated method stub
Object temp = a[x];
a[x] = a[y];
a[y] = temp;
}
/**
* 交换两个数据
*
* @param a
* 数组
* @param x
* 数组下标1
* @param y
* 数组下标2
*/
private static void swap(int[] a, int x, int y) {
// TODO Auto-generated method stub
int temp = a[x];
a[x] = a[y];
a[y] = temp;
}
/**
* 打印数组
*
* @param a
* Object类型数组
*/
public static void print(Object[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
/**
* 打印数组
*
* @param a
* int类型数组
*/
public static void print(int[] a) {
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println();
}
}
/**通过高度比较器和重量比较器,我们就可以灵活的选择比较的依据,这时,如果,需求再有变化,按照Cat的年龄进行排序时,只需要新建一个年龄比较器,然后为Cat类的Comparator的set方法或者Cat的构造方法传入新建的年龄比较器,这样不需再改动其他代码,就可以实现Cat类型的数组按新定义的年龄进行排序,最大限度地重用了代码。
* 测试类
* @author jesson
*
*/
public class Test {
public static void main(String[] args) {
//int[] a = new int[]{9,8,2,4,5,6,7};
Cat[] a = {new Cat(5,5),new Cat(1,1),new Cat(3,3)};
//Dog[] a = {new Dog(3),new Dog(2),new Dog(6)};
DataSorter.print(a);
DataSorter.bubbleSort(a);
DataSorter.print(a);
}
}