JAVA-作业4-1:设计完成Human类和Student类,要求能够实现以下功能

题目:

设计完成Human类和Student类,要求能够实现以下功能:

  1. 两类之间是继承关系;
  2. 两类中均需有构造函数;
  3. 重写equals( ), toString( ), hashCode( )方法;
  4. 测试方法中需体现多态和强制类型转换。

代码如下:

1.Human类:

package HW1029;

import java.util.Objects;

public class Human {
    private String name;
    private int age;
    private String sex;

    public Human() {
    }

    public Human(String name, int age, String sex) {
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    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 String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {// 先比较两个对象的内存地址,如果地址相同则返回true
            return true;
        }
        if (o instanceof Human) {// 如果o是Human类,比较两个对象的值,完全相等则返回true
            Human h = (Human) o;
            if ((getAge() == h.getAge()) && (getName().equals(h.getName())) && (getSex().equals(h.getSex()))) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    // 重写equals方法就必须重写hashCode方法
    // 对象的物理地址转换成一个整数,然后该整数通过hash函数的算法就得到了hashcode
    // 如果重写了equals方法却没有重写hashCode方法,则可能会导致两个完全相同的对象存储在hash表的不同位置
    @Override
    public int hashCode() {
        return Objects.hash(getAge(), getName(), getSex());
    }

    @Override
    public String toString() {
        return "Human{" + "name: " + getName() + ",age: " + getAge() + ",sex: " + getSex() + "}\n";
    }
}

2.Student类:

package HW1029;

import java.util.Objects;

public class Student extends Human {
    private String stuId;

    public Student() {
    }

    public Student(String name, int age, String sex, String stuId) {
        super(name, age, sex);
        this.stuId = stuId;
    }

    public String getStuId() {
        return stuId;
    }

    public void setStuId(String stuId) {
        this.stuId = stuId;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o instanceof Student) {
            if (super.equals(o)) {
                Student st = (Student) o;
                if (getStuId().equals(st.getStuId())) {
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public int hashCode() {
        return Objects.hash(super.hashCode(), getStuId());
    }

    @Override
    public String toString() {
        return "Student{" + "name: " + getName() + ",age: " + getAge() + ",sex: " + getSex() + ",stuId: "
                + getStuId() + "}\n";
    }
}

3.Test:

package HW1029;

public class Test {
    public static void main(String[] args) {
        Student stu1 = new Student("xiaoming", 18, "male", "001");
        Student stu2 = new Student("xiaoming", 18, "male", "001");
        Student stu3 = new Student("xiaowang", 20, "famale", "002");
        System.out.println("stu1 : " + stu1 + " hashCode is: " + stu1.hashCode());
        System.out.println("stu2 : " + stu2 + " hashCode is: " + stu2.hashCode());
        System.out.println("stu3 : " + stu3 + " hashCode is: " + stu3.hashCode());
        if (stu1.equals(stu2)) {
            System.out.println("\n==" + stu1 + "equals to\n" + stu2 + "\n");
        } else {
            System.out.println("\n==" + stu1 + "don't equals to\n" + stu2 + "\n");
        }
        if (stu1.equals(stu3)) {
            System.out.println("==" + stu1 + "equals to\n" + stu3 + "\n");
        } else {
            System.out.println("==" + stu1 + "don't equals to\n" + stu3 + "\n");
        }
    }
}

运行结果:

JAVA-作业4-1:设计完成Human类和Student类,要求能够实现以下功能_第1张图片

你可能感兴趣的:(JAVA,java,哈希算法,开发语言)