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 }

 

posted on 2018-12-04 23:00  从零开始-白  阅读(226)  评论(0编辑  收藏  举报