班级学生排队,男生按照身高从低到高排一队,女生,按照姓名从小到大排一队(使用接口和匿名内部类)
package homework;
import java.sql.Connection;
import java.util.Arrays;
/*
班级学生排队,男生按照身高从低到高排一队,女生,按照姓名从小到大排一队(使用接口和匿名内部类)
分析:
接口,数组,枚举,匿名内部类,Arrrys工具类,String的方法
实现过程:
1.分组
按照性别分成男队和女队
2.排序
使用接口制定对应的规则分别实现男队和女队的排序
只要是从几个固定的值中取出一个,我们可以采用在接口中创建属性的方法,例如当前的Constant.java中的接口.
思想理解:
在进行代码编写时,不能乱使用面向对象,原则:谁的活儿谁干,不是你的活儿不要干.
*/
public class Demo1 {
public static void main(String[] args) {
Student[][] students = test1();
test2(students[0]);
test3(students[1]);
}
//1.分组
public static Student[][] test1(){
//组织数据
Student[] students = {
new Student("zhangsan",170.0,Gender.Male),
new Student("zhangfei",180.0,Gender.Male),
new Student("zhaoyun",174.0,Gender.Male),
new Student("guanyu",190.0,Gender.Male),
new Student("liubei",167.0,Gender.Male),
new Student("caocao",185.0,Gender.Male),
new Student("yuanshao",171.0,Gender.Male),
new Student("yuanshu",172.0,Gender.Male),
new Student("zhouyu",174.0,Gender.Male),
new Student("huanggai",182.0,Gender.Male),
new Student("xiaoqiao",151.0,Gender.Female),
new Student("daqiao",153.0,Gender.Female),
new Student("diaochan",171.0,Gender.Female),
new Student("sunshangxiang",163.0,Gender.Female),
new Student("huangyueying",169.0,Gender.Female)
};
//分组
//男队
Student[] stu_males = new Student[students.length];
//女队
Student[] stu_females = new Student[students.length];
//设置男队的标识
int x = 0;
//女队的标识
int y = 0;
for (int i = 0; i < students.length; i++) {
Student student = students[i];
if (student.getGender() == Gender.Male){
stu_males[x] = student;
x++;
}else {
stu_females[y] = student;
y++;
}
}
Student[] new_Stu_Males = Arrays.copyOf(stu_males,x);
Student[] new_Stu_Females = Arrays.copyOf(stu_females,y);
System.out.println(Arrays.toString(new_Stu_Males));
System.out.println(Arrays.toString(new_Stu_Females));
return new Student[][]{new_Stu_Males,new_Stu_Females};
}
//2.给男生排序---身高
public static void test2(Student[] males){
//第一种方法:先创建一个独立的给男生按照身高比较的比较器,再把比较器交给queue方法
ComWithMale comWithMale = new ComWithMale();
ArrayTools.queue(males,comWithMale);
//第二种:直接使用匿名内部类,编写比较的规则-男生按照身高比较,再交给queue方法
// ArrayTools.queue(males, new MyComparator() {
// @Override
// public int compare(Object o1, Object o2) {
// //容错处理
// //不是学生类
// if (!(o1 instanceof Student)){
// //抛出异常
// throw new ClassCastException("当前的不是Student类");
// }
// if (!(o2 instanceof Student)){
// //抛出异常
// throw new ClassCastException("当前的不是Student类");
// }
//
// //向下转型
// Student s1 = (Student) o1;
// Student s2 = (Student) o2;
//
// //按照身高比
// return (int)Math.ceil(s1.getHeight()-s2.getHeight());
// }
// });
System.out.println("排序后:"+Arrays.toString(males));
}
//3.给女生排序---姓名
public static void test3(Student[] females){
ArrayTools.queue(females, new MyComparator() {
@Override
//自己写按照姓名比较
public int compare(Object o1, Object o2) {
// //容错处理
//不是学生类
if (!(o1 instanceof Student)){
//抛出异常
throw new ClassCastException("当前的不是Student类");
}
if (!(o2 instanceof Student)){
//抛出异常
throw new ClassCastException("当前的不是Student类");
}
//向下转型
Student s1 = (Student) o1;
Student s2 = (Student) o2;
//按照姓名比较
return s1.getName().compareTo(s2.getName());
}
});
System.out.println("排序后:"+Arrays.toString(females));
}
}
//创建一个用于比较学生的类-确定比较男生--按照身高比--比较器
class ComWithMale implements MyComparator{
@Override
public int compare(Object o1, Object o2) {
// //容错处理
//不是学生类
if (!(o1 instanceof Student)){
//抛出异常
throw new ClassCastException("当前的不是Student类");
}
if (!(o2 instanceof Student)){
//抛出异常
throw new ClassCastException("当前的不是Student类");
}
//向下转型
Student s1 = (Student) o1;
Student s2 = (Student) o2;
//按照身高比
double v = s1.getHeight()-s2.getHeight();
if (v>=0){
return (int)Math.ceil(v);
}else {
return (int)Math.floor(v);
}
}
}
//创建表示性别的枚举
//表示可以取几个固定值的地方
//枚举是在高版本的jdk中替换接口属性(符号常量)的功能.
enum Gender{
Male,Female
}
//建立学生类
class Student{
private String name;
private double height;
//这里使用的是通过接口设置的符号常量
//private int sex = Constant.SEX_MALE;
//通过枚举设置的常量
private Gender gender;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", height=" + height +
", gender=" + gender +
'}';
}
public Student(){}
public Student(String name, double height, Gender gender) {
this.name = name;
this.height = height;
this.gender = gender;
}
}
package homework;
public class ArrayTools {
//排序
public static void queue(Student[] students,MyComparator comparator){
for (int i = 0; i < students.length-1; i++) {
for (int j = 0; j < students.length-i-1; j++) {
//按照身高从低到高
if (comparator.compare(students[j],students[j+1])>0){
//交换的是学生
Student temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}
}
}
}
}
package homework;
public interface MyComparator {
/*
方法:比较的是任意两个学生
返回值:int型,可以表示正,负,0
正:表示前面的大于后面
负:后面的大于前面的
0:前和后相等
*/
public int compare(Object o1,Object o2);
}