JAVA中如何根据类的属性进行排序,然后输出类的所有信息,求大神解答。 5
定义一个Person类。属性包括姓名,性别,年龄,行为包括说话(返回Person基本信息)构造方法带有三个参数;并封装属性定义一个employee类,为Person类的子...
定义一个Person类。
属性包括姓名,性别,年龄,
行为包括说话(返回Person基本信息)
构造方法带有三个参数;并封装属性
定义一个employee类,为Person类的子类,
属性包括工号,工资,工龄
行为包括说话(返回员工所有基本信息)
构造方法为带参构造方法,属性封装
测试类:定义三个员工,要求
根据总工资(总工资=工资+工龄*20)由少到多排序显示员工所有信息
class Person
{
protected String name;
protected String sex;
protected int age;
public Person(String na,String se,int ag)
{
setName(na);
setSex(se);
setAge(ag);
}
public void setName(String n)
{
this.name=n;
}
public String getName()
{
return this.name;
}
public void setSex(String s)
{
if(s=="female"||s=="male")
this.sex=s;
else this.sex="the sex is illegal";
}
public String getSex()
{
return this.sex;
}
public void setAge(int a)
{
if(a>0)
this.age=a;
else a=0;
}
public int getAge()
{
return this.age;
}
public void speak()
{
System.out.println("姓名:"+this.name+"性别:"+this.sex+"年龄:"+this.age);
}
}
class employee extends Person
{
private String gonghao;
private double salary;
private int gongling;
public employee(String n,String s,int a,String gongh,double sa,int gongl)
{
super(n,s,a);
setGonghao(gongh);
setSalary(sa);
setGongling(gongl);
}
public void setGonghao(String gh)
{
this.gonghao = gh;
}
public String getGonghao()
{
return this.gonghao;
}
public void setSalary(double s)
{
if(s>0)
this.salary =s;
else this.salary =0;
}
public double getSalary()
{
return this.salary;
}
public void setGongling(int gl)
{
if(gl>0)
this.gongling=gl;
else this.gongling =0;
}
public int getGongling()
{
return this.gongling;
}
public void speak()
{
System.out.println("姓名:"+super.name+"性别:"+super.sex+"年龄:"+super.age+"工号:"+this.gonghao+"工资:"+this.salary+"工龄:"+this.gongling);
}
public double zgz()
{
return (this.salary+20*this.gongling);
}
}
class zhigong
{
public static void main(String[] args)
{
employee e[] = new int[3]
employee e[0] = new employee("xiaoming","female",24,"001",3500,5);
employee e[1] = new employee("xiaoli","male",25,"002",4000,10);
employee e[2] = new employee("xiaohong","female",30,"003",4000,15);
}
} 展开
属性包括姓名,性别,年龄,
行为包括说话(返回Person基本信息)
构造方法带有三个参数;并封装属性
定义一个employee类,为Person类的子类,
属性包括工号,工资,工龄
行为包括说话(返回员工所有基本信息)
构造方法为带参构造方法,属性封装
测试类:定义三个员工,要求
根据总工资(总工资=工资+工龄*20)由少到多排序显示员工所有信息
class Person
{
protected String name;
protected String sex;
protected int age;
public Person(String na,String se,int ag)
{
setName(na);
setSex(se);
setAge(ag);
}
public void setName(String n)
{
this.name=n;
}
public String getName()
{
return this.name;
}
public void setSex(String s)
{
if(s=="female"||s=="male")
this.sex=s;
else this.sex="the sex is illegal";
}
public String getSex()
{
return this.sex;
}
public void setAge(int a)
{
if(a>0)
this.age=a;
else a=0;
}
public int getAge()
{
return this.age;
}
public void speak()
{
System.out.println("姓名:"+this.name+"性别:"+this.sex+"年龄:"+this.age);
}
}
class employee extends Person
{
private String gonghao;
private double salary;
private int gongling;
public employee(String n,String s,int a,String gongh,double sa,int gongl)
{
super(n,s,a);
setGonghao(gongh);
setSalary(sa);
setGongling(gongl);
}
public void setGonghao(String gh)
{
this.gonghao = gh;
}
public String getGonghao()
{
return this.gonghao;
}
public void setSalary(double s)
{
if(s>0)
this.salary =s;
else this.salary =0;
}
public double getSalary()
{
return this.salary;
}
public void setGongling(int gl)
{
if(gl>0)
this.gongling=gl;
else this.gongling =0;
}
public int getGongling()
{
return this.gongling;
}
public void speak()
{
System.out.println("姓名:"+super.name+"性别:"+super.sex+"年龄:"+super.age+"工号:"+this.gonghao+"工资:"+this.salary+"工龄:"+this.gongling);
}
public double zgz()
{
return (this.salary+20*this.gongling);
}
}
class zhigong
{
public static void main(String[] args)
{
employee e[] = new int[3]
employee e[0] = new employee("xiaoming","female",24,"001",3500,5);
employee e[1] = new employee("xiaoli","male",25,"002",4000,10);
employee e[2] = new employee("xiaohong","female",30,"003",4000,15);
}
} 展开
1个回答
展开全部
您好,您这样:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 作者: 版本: 要求:设计一个具备比较功能的类(例如 员工类, 需要有姓名, 年龄, 薪水三个成员属性,需要私有并提供get, set方法,
* 可以通过构造函数进行 初始化,并且按照薪水进行排序)。
*/
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = 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 this.name + " , " + this.getAge() + " , " + this.salary;
}
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();
list.add(e1);
list.add(e2);
list.add(e3);
Comparator comp = new ComparatorImpl();
Collections.sort(list, comp);
for (Employee e : (List<Employee>)list) {
System.out.println(e);
}
}
}
class ComparatorImpl implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
int salary1 = e1.getSalary();
int salary2 = e2.getSalary();
if (salary1 > salary2) {
return 1;
} else if (salary1 < salary2) {
return -1;
} else {
return 0;
}
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 作者: 版本: 要求:设计一个具备比较功能的类(例如 员工类, 需要有姓名, 年龄, 薪水三个成员属性,需要私有并提供get, set方法,
* 可以通过构造函数进行 初始化,并且按照薪水进行排序)。
*/
public class Employee {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = 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 this.name + " , " + this.getAge() + " , " + this.salary;
}
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();
list.add(e1);
list.add(e2);
list.add(e3);
Comparator comp = new ComparatorImpl();
Collections.sort(list, comp);
for (Employee e : (List<Employee>)list) {
System.out.println(e);
}
}
}
class ComparatorImpl implements Comparator<Employee> {
public int compare(Employee e1, Employee e2) {
int salary1 = e1.getSalary();
int salary2 = e2.getSalary();
if (salary1 > salary2) {
return 1;
} else if (salary1 < salary2) {
return -1;
} else {
return 0;
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询