首页 > 其他 > 详细

重写equals方法

时间:2018-12-05 01:10:40      阅读:129      评论:0      收藏:0      [点我收藏+]
 1 package com.oop.inhert;
 2 
 3 public class Student {
 4     private int id;
 5     private String name;
 6     private int age;
 7     
 8     public Student() {}
 9     
10     public Student(int id, String name, int age) {
11         this.id = id;
12         this.name = name;
13         this.age = age;
14     }
15     public int getId() {
16         return id;
17     }
18     public void setId(int id) {
19         this.id = id;
20     }
21     public String getName() {
22         return name;
23     }
24     public void setName(String name) {
25         this.name = name;
26     }
27     public int getAge() {
28         return age;
29     }
30     public void setAge(int age) {
31         this.age = age;
32     }
33 
34     /**
35      * 重写equals()方法,如果两名学员的学号和姓名相同,则为同一对象
36      * @param stu
37      * @return
38      */
39     public boolean equals(Student stu) {
40         return this.id==stu.id && this.name.equals(stu.name);
41     }
42 
43     
44 }
 1 package com.oop.inhert;
 2 
 3 public class Test {
 4     public static void main(String[] args) {
 5         Student stu1=new Student(1,"张三",18);
 6         Student stu2=new Student(1,"张三",18);
 7         
 8         System.out.println(stu1.equals(stu2));
 9     }
10     
11 }

 

重写equals方法

原文:https://www.cnblogs.com/baichang/p/10068219.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!