
您好,您这样:
import javautilArrayList;
import javautilCollections;
import javautilComparator;
import javautilList;
/
作者: 版本: 要求:设计一个具备比较功能的类(例如 员工类, 需要有姓名, 年龄, 薪水三个成员属性,需要私有并提供get, set方法,
可以通过构造函数进行 初始化,并且按照薪水进行排序)。
/
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
thisname = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
thisage = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
thissalary = salary;
}
private int age;
private int salary;
public Employee(String na, int ag, int sa) {
name = na;
age = ag;
salary = sa;
}
@Override
public String toString() {
return thisname + " , " + thisgetAge() + " , " + thissalary;
}
public static void main(String args[]) {
Employee e1 = new Employee("霸气外漏老总", 35, 20000);
Employee e2 = new Employee("疯牛涕淌小三", 29, 7000);
Employee e3 = new Employee("万年屌丝基佬", 27, 4700);
List list = new ArrayList();
listadd(e1);
listadd(e2);
listadd(e3);
Comparator comp = new ComparatorImpl();
Collectionssort(list, comp);
for (Employee e : (List<Employee>)list) {
Systemoutprintln(e);
}
}
}
class ComparatorImpl implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
int salary1 = e1getSalary();
int salary2 = e2getSalary();
if (salary1 > salary2) {
return 1;
} else if (salary1 < salary2) {
return -1;
} else {
return 0;
}
}
}
以上就是关于JAVA中如何根据类的属性进行排序,然后输出类的所有信息,求大神解答。全部的内容,包括:JAVA中如何根据类的属性进行排序,然后输出类的所有信息,求大神解答。、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)