From ee1a314d2a4ebe7047b8ba881232982c863769a2 Mon Sep 17 00:00:00 2001 From: bingor_yhj Date: Thu, 6 Oct 2022 18:31:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98(=E4=B8=80?= =?UTF-8?q?=E6=9C=9F)-=E8=AE=BE=E8=AE=A1=E6=A8=A1=E5=BC=8F=E7=89=88-?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/com/msb/PropertyMgr.java | 4 +- src/com/msb/strategy/Cat.java | 30 +++++++++++++++ src/com/msb/strategy/Cat2.java | 34 +++++++++++++++++ src/com/msb/strategy/CatHeightComparator.java | 16 ++++++++ src/com/msb/strategy/CatWeightComparator.java | 16 ++++++++ src/com/msb/strategy/Comparable.java | 11 ++++++ src/com/msb/strategy/Comparator.java | 11 ++++++ src/com/msb/strategy/Dog.java | 30 +++++++++++++++ src/com/msb/strategy/Sorter.java | 37 ++++++++++++++++++ src/com/msb/strategy/Sorter2.java | 38 +++++++++++++++++++ 10 files changed, 226 insertions(+), 1 deletion(-) create mode 100644 src/com/msb/strategy/Cat.java create mode 100644 src/com/msb/strategy/Cat2.java create mode 100644 src/com/msb/strategy/CatHeightComparator.java create mode 100644 src/com/msb/strategy/CatWeightComparator.java create mode 100644 src/com/msb/strategy/Comparable.java create mode 100644 src/com/msb/strategy/Comparator.java create mode 100644 src/com/msb/strategy/Dog.java create mode 100644 src/com/msb/strategy/Sorter.java create mode 100644 src/com/msb/strategy/Sorter2.java diff --git a/src/com/msb/PropertyMgr.java b/src/com/msb/PropertyMgr.java index 50e5351..407f95b 100644 --- a/src/com/msb/PropertyMgr.java +++ b/src/com/msb/PropertyMgr.java @@ -12,7 +12,9 @@ import java.util.Properties; */ public class PropertyMgr { - private static Properties props = new Properties(); + private static final Properties props = new Properties(); + + private PropertyMgr() {} static { try { diff --git a/src/com/msb/strategy/Cat.java b/src/com/msb/strategy/Cat.java new file mode 100644 index 0000000..f7a002f --- /dev/null +++ b/src/com/msb/strategy/Cat.java @@ -0,0 +1,30 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:12 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class Cat implements Comparable { + + private int weight, height; + + public Cat(int weight) { + this.weight = weight; + } + + @Override + public int compareTo(Cat o) { + if(this.weight < o.weight) return -1; + else if(this.weight > o.weight) return 1; + else return 0; + } + + @Override + public String toString() { + return "Cat{" + + "weight=" + weight + + '}'; + } +} diff --git a/src/com/msb/strategy/Cat2.java b/src/com/msb/strategy/Cat2.java new file mode 100644 index 0000000..116c232 --- /dev/null +++ b/src/com/msb/strategy/Cat2.java @@ -0,0 +1,34 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:12 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class Cat2 { + + private int weight, height; + + public Cat2(int weight, int height) { + this.weight = weight; + this.height = height; + } + + @Override + public String toString() { + return "Cat2{" + + "weight=" + weight + + ", height=" + height + + '}'; + } + + public int getWeight() { + return weight; + } + + public int getHeight() { + return height; + } + +} diff --git a/src/com/msb/strategy/CatHeightComparator.java b/src/com/msb/strategy/CatHeightComparator.java new file mode 100644 index 0000000..bd6021d --- /dev/null +++ b/src/com/msb/strategy/CatHeightComparator.java @@ -0,0 +1,16 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:56 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class CatHeightComparator implements Comparator { + @Override + public int compare(Cat2 o1, Cat2 o2) { + if(o1.getHeight() < o2.getHeight()) return -1; + else if(o1.getHeight() > o2.getHeight()) return 1; + else return 0; + } +} diff --git a/src/com/msb/strategy/CatWeightComparator.java b/src/com/msb/strategy/CatWeightComparator.java new file mode 100644 index 0000000..537fadf --- /dev/null +++ b/src/com/msb/strategy/CatWeightComparator.java @@ -0,0 +1,16 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:56 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class CatWeightComparator implements Comparator { + @Override + public int compare(Cat2 o1, Cat2 o2) { + if(o1.getWeight() < o2.getWeight()) return -1; + else if(o1.getWeight() > o2.getWeight()) return 1; + else return 0; + } +} diff --git a/src/com/msb/strategy/Comparable.java b/src/com/msb/strategy/Comparable.java new file mode 100644 index 0000000..ebc13f9 --- /dev/null +++ b/src/com/msb/strategy/Comparable.java @@ -0,0 +1,11 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:05 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public interface Comparable { + int compareTo(T o); +} diff --git a/src/com/msb/strategy/Comparator.java b/src/com/msb/strategy/Comparator.java new file mode 100644 index 0000000..35429a9 --- /dev/null +++ b/src/com/msb/strategy/Comparator.java @@ -0,0 +1,11 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:50 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public interface Comparator { + public int compare(T o1, T o2); +} diff --git a/src/com/msb/strategy/Dog.java b/src/com/msb/strategy/Dog.java new file mode 100644 index 0000000..0e9333e --- /dev/null +++ b/src/com/msb/strategy/Dog.java @@ -0,0 +1,30 @@ +package com.msb.strategy; + +/** + * @Author bingor + * @Date 2022-10-06 17:12 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class Dog implements Comparable { + + private int food; + + public Dog(int food) { + this.food = food; + } + + @Override + public int compareTo(Dog o) { + if(this.food < o.food) return -1; + else if(this.food > o.food) return 1; + else return 0; + } + + @Override + public String toString() { + return "Dog{" + + "food=" + food + + '}'; + } +} diff --git a/src/com/msb/strategy/Sorter.java b/src/com/msb/strategy/Sorter.java new file mode 100644 index 0000000..abb3d50 --- /dev/null +++ b/src/com/msb/strategy/Sorter.java @@ -0,0 +1,37 @@ +package com.msb.strategy; + +import java.util.Arrays; + +/** + * @Author bingor + * @Date 2022-10-06 17:06 + * @Description: com.msb.strategy + * @Version: 1.0 + */ +public class Sorter { + + public void sort(Comparable[] arr) { + for(int i=0; i { + + public void sort(T[] arr, Comparator comparator) { + for(int i=0; i sorter = new Sorter2(); + sorter.sort(cats, comparator); + System.out.println(Arrays.toString(cats)); + } + +}