添加3个类,分别实现IComparer接口,实现对Student类的三个字段的排序。
1、学生类:学号、姓名、年龄2、请选择:1、添加学生信息。2、删除学生信息2、查询学生信息。3、重复的学号不能添加。4、查询学生信息功能中有:1、查询所有(按学号排序)2、查询所有(按姓名排序),2、查询所有(按年龄排序)4、按学号查询(查没有,则打印查无此学生)5、退出
学生类
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_01{classStudent{publicstringNum{get;set;}//学号publicstringName{get;set;}//姓名publicintAge{get;set;}//年龄//创建带参的构造方法publicStudent(stringnum,stringname,intage){this.Num=num;this.Name=name;this.Age=age;}//创建无参的构造方法,在本次代码中未使用publicStudent(){}//重写了Tostring()的方法将字典中的值输出打印publicoverridestringToString(){return$"学号:{Num}姓名:{Name}年龄:{Age}";}//创建比较器用于比较publicintCompareTo(Studentother){returnthis.Num.CompareTo(other.Num);}}}工具类(Utility)
工具类中封装了一些方法用于调用,使得主方法中的代码简洁
创建三个比较器用于比较排序,使用IComparer<>接口
1.年龄比较器
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_01{classNameSort:IComparer
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_01{classNameSort:IComparer
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespace_01{//构造比较器,进行比较classNumSort:IComparer
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。