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

代码示例展示了如何在Java中创建Human类和Student类,其中Student类继承自Human类。这两个类都实现了构造函数,重写了equals()、hashCode()和toString()方法。在main方法中,通过实例化对象并进行比较,体现了多态特性和强制类型转换。
摘要由CSDN通过智能技术生成

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

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

package Human_Student;

import java.util.Objects;


public class Extend {
    public static void main(String[] args) {
        Student stu1=new Student("xiaoming",12,"man","one");
        Student stu2=new Student("xiaoming",12,"man","one");
        Student stu3=new Student("xiaowang",22,"woman","ten");
        System.out.println("==stu1 is: "+stu1+"\n hashCode is: "+stu1.hashCode());
        System.out.println("==stu2 is: "+stu2+"\n hashCode is: "+stu2.hashCode());
        System.out.println("==stu3 is: "+stu3+"\n hashCode is: "+stu3.hashCode());
        if(stu1.equals(stu2)){
            System.out.println("=="+stu1+"\n equals to\n"+stu2+"\n\n");
        }
        else{
            System.out.println("=="+stu1+"\n don't equals to\n"+stu2+"\n\n");
        }
        if(stu1.equals(stu3)){
            System.out.println("=="+stu1+"\n equals to\n"+stu3+"\n\n");
        }
        else{
            System.out.println("=="+stu1+"\n don't equals to\n"+stu3+"\n\n");
        }
    }
}

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

    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) return true;
        if (!(o instanceof Human)) return false;
        Human human = (Human) o;
        return getAge() == human.getAge() && Objects.equals(getName(), human.getName()) && Objects.equals(getSex(), human.getSex());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getName(), getAge(), getSex());
    }

    @Override
    public String toString() {
        return "Human{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

class Student extends Human{
    private String grade;

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

    public String getGrade() {
        return grade;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof Student)) return false;
        if (!super.equals(o)) return false;
        Student student = (Student) o;
        return Objects.equals(getGrade(), student.getGrade());
    }

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

    public void setGrade(String grade) {
        this.grade = grade;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + getName() + '\'' +
                ", age=" + getAge() +
                ", sex='" + getSex() + '\'' +
                "grade='" + grade + '\'' +
                '}';
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆呆水獭_(:_」∠)_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值