parent
27b0107b7a
commit
70f9cb6801
@ -0,0 +1,4 @@
|
||||
package com.example.tankbattle.singleton;
|
||||
|
||||
public class Singleton {
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
|
||||
public class Cat implements CustomComparable<Cat> {
|
||||
private String id;
|
||||
private String name;
|
||||
private int weight;
|
||||
private int height;
|
||||
|
||||
public Cat(int weight, int height) {
|
||||
this.weight = weight;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int getWeight() {
|
||||
return weight;
|
||||
}
|
||||
|
||||
public void setWeight(int weight) {
|
||||
this.weight = weight;
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
|
||||
public void setHeight(int height) {
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
public int compareToa(Cat cat) {
|
||||
if (this.weight < cat.weight) {
|
||||
return -1;
|
||||
} else if (this.weight > cat.weight) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int compareTo(Cat cat) {
|
||||
if (this.weight < cat.weight) {
|
||||
return -1;
|
||||
} else if (this.weight > cat.weight) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
public interface CustomComparable<T> {
|
||||
int compareTo(T o);
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
public interface CustomComparator<T> {
|
||||
int compare(T o1, T o2);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
public class Dog implements CustomComparable<Dog> {
|
||||
|
||||
private int food;
|
||||
|
||||
public int getFood() {
|
||||
return food;
|
||||
}
|
||||
|
||||
public void setFood(int food) {
|
||||
this.food = food;
|
||||
}
|
||||
|
||||
public Dog(int food) {
|
||||
this.food = food;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Dog dog) {
|
||||
if (this.food < dog.food) {
|
||||
return -1;
|
||||
} else if (this.food < dog.food) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Dog{" +
|
||||
"food=" + food +
|
||||
'}';
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
public class DogComparator implements CustomComparator<Dog> {
|
||||
|
||||
@Override
|
||||
public int compare(Dog dog1, Dog dog2) {
|
||||
if (dog1.getFood() < dog2.getFood()) {
|
||||
return -1;
|
||||
} else if (dog1.getFood() > dog2.getFood()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class Main {
|
||||
private static int[] arr = {9, 6, 7, 7, 6, 5, 5, 4, 4};
|
||||
|
||||
private static Sorter sorter = new Sorter();
|
||||
public static void main(String[] args) {
|
||||
Cat[] cats = {new Cat(1, 1), new Cat(3, 3), new Cat(5, 5)};
|
||||
Dog[] dogs = {new Dog(5), new Dog(3), new Dog(1)};
|
||||
sorter.sort(dogs, new CustomComparator<Dog>() {
|
||||
@Override
|
||||
public int compare(Dog dog1, Dog dog2) {
|
||||
if (dog1.getFood() < dog2.getFood()) {
|
||||
return -1;
|
||||
} else if (dog1.getFood() > dog2.getFood()) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
});
|
||||
System.out.println(Arrays.asList(dogs));
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package com.example.tankbattle.strategy;
|
||||
|
||||
public class Sorter<T> {
|
||||
|
||||
public void sort(T[] arr, CustomComparator<T> comparator) {
|
||||
int length = arr.length;
|
||||
if (arr == null || length < 2) {
|
||||
return;
|
||||
}
|
||||
|
||||
T tmp;
|
||||
for (int i = length - 1; i >= 0; i--) {
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (comparator.compare(arr[j + 1], (arr[j])) < 0) {
|
||||
tmp = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue