用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

实现Student类的equals重载函数

2019-07-18 作者: Ryan2019举报

[java]代码库

//测试类
public class StudentDemo {
 
    public static void main(String[] args) {
        Student s1 = new Student("000","张三",18);
        Student s2 = new Student("000","张三",18);//随便改
        boolean flag = s1.equals(s2);
        System.out.println(flag);
    }
}
 
//实现类Student
public class Student {
 
    //私有成员
    private String id;
    private String name;
    private int age;
 
    //带参构造函数
    public Student(String id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
 
    //无参构造函数
    public Student() {
        super();
        // TODO Auto-generated constructor stub
    }
 
    public String getId() {
        return id;
    }
 
    public void setId(String id) {
        this.id = id;
    }
 
    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;
    }
 
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
    }
 
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass()) 
            return false;
        Student s = (Student) obj;
        if (id != s.id) 
            return false;
        if (age != s.age) 
            return false;
        if (name == null) {
            if (s.name != null)
                return false;
            } else if (!name.equals(s.name))
                return false;
        return true;
    }
 
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...