实现可比较的类

时间:2015-03-08 21:04:03

标签: java comparable

我是一个类我认识的人我可以使它成为可比的覆盖compareTo方法等。我有一个任务来创建可以比较这些人的花药类。

public class Person {
    private String name;
    private String lastName;
    private int age;
    private String middleName;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMiddleName() {
        return middleName;
    }

    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }

    public Person(){}   


    @Override
    public int hashCode() {
        return this.lastName.hashCode() + this.name.hashCode() + this.age;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (age != other.age)
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        if (middleName == null) {
            if (other.middleName != null)
                return false;
        } else if (!middleName.equals(other.middleName))
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }

    public String getNama() {
        return name;
    }
    public void setNama(String nama) {
        this.name = nama;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}

0 个答案:

没有答案