package cn.edu.hpu.Strategy; public class Test { public static void main(String[] args) { int[] a={9,5,3,7,1}; DataSorter.sort(a); DataSorter.p(a); } }
package cn.edu.hpu.Strategy; public class DataSorter { public static void sort(int[] a) { for (int i = a.length; i >0; i--) { for (int j = 0; j < i-1; j++) { if(a[j]>a[j+1]){ swap(a,j,j+1); } } } } private static void swap(int[] a, int x, int y) { int temp=a[x]; a[x]=a[y]; a[y]=temp; } public static void p(int[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } System.out.println(); } }
1 3 5 7 9
package cn.edu.hpu.Strategy; public class Dog { //狗的身高 private int height; //狗的体重 private int weight; public Dog(int height, int weight) { super(); 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; } }
public static void sort(Dog[] a) { for (int i = a.length; i >0; i--) { for (int j = 0; j < i-1; j++) { if(a[j].getHeight()>a[j+1].getHeight()){ swap(a,j,j+1); } } } } private static void swap(Dog[] a, int x, int y) { Dog temp=a[x]; a[x]=a[y]; a[y]=temp; } public static void p(Dog[] a) { for (int i = 0; i < a.length; i++) { System.out.print(i+"号狗的高度:"+a[i].getHeight()); } System.out.println(); }
package cn.edu.hpu.Strategy; public class Cat { //猫的饭量 private int food; public Cat(int food) { super(); this.food = food; } public int getFood() { return food; } public void setFood(int food) { this.food = food; } }
package cn.edu.hpu.Strategy; public interface Comparable { /*实现这个接口的对象使用这个方法进行比较时, *返回1是比那个对象大,返回0是相等,返回-1是比那个对象小*/ public int compareTo(Object o); }
package cn.edu.hpu.Strategy; public class Dog implements Comparable{ //狗的身高 private int height; //狗的体重 private int weight; //省略部分代码。。。 @Override public int compareTo(Object o) { if(o instanceof Dog){ Dog d=(Dog)o; if(this.getHeight()>d.getHeight()) return 1; else if(this.getHeight()<d.getHeight()) return -1; else return 0; } //不属于Dog类,不能进行比较,这里要抛异常的,为了简单起见,返回一个-100 return -100; } }
<pre name="code" class="java">public static void sort(Object[] a) { for (int i = a.length; i >0; i--) { for (int j = 0; j < i-1; j++) { Comparable o1=(Comparable)a[j]; Comparable o2=(Comparable)a[j+1]; if(o1.compareTo(o2)==1){ swap(a,j,j+1); } } } } private static void swap(Object[] a, int x, int y) { Object temp=a[x]; a[x]=a[y]; a[y]=temp; } public static void p(Object[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i]+" "); } System.out.println(); }
@Override public String toString() { return this.getHeight()+"|"+this.getWeight(); }
package cn.edu.hpu.Strategy; public class Test { public static void main(String[] args) { Dog[] dogs={new Dog(3,3),new Dog(5,5),new Dog(1,1)}; DataSorter.sort(dogs); DataSorter.p(dogs); } }测试结果:
package cn.edu.hpu.Strategy; public class Cat implements Comparable{ //猫的饭量 private int food; public Cat(int food) { super(); this.food = food; } public int getFood() { return food; } public void setFood(int food) { this.food = food; } @Override public int compareTo(Object o) { if(o instanceof Cat){ Cat c=(Cat)o; if(this.getFood()>c.getFood()) return 1; else if(this.getFood()<c.getFood()) return -1; else return 0; } //不属于Cat类,不能进行比较,这里要抛异常的,为了简单起见,返回一个-100 return -100; } @Override public String toString() { return this.getFood()+" "; } }
package cn.edu.hpu.Strategy; public class Test { public static void main(String[] args) { Cat[] cats={new Cat(21),new Cat(15),new Cat(9)}; DataSorter.sort(cats); DataSorter.p(cats); } }
刚刚只是热身,我们下一篇总结继续探讨策略模式。
转载请注明出处:http://blog.csdn.net/acmman/article/details/46634355