1 class Person {
 2     private String name;
 3     private String location;
 4     
 5     Person(String name) {
 6         this.name = name;
 7         location = "beijing";
 8     }
 9     
10     Person(String name, String location) {
11         this.name = name;
12         this.location = location;
13     }
14     
15     public String toString() {
16         return "Person Class. ";
17     }
18     
19     public boolean equals(Object o) {
20         if(o == null) {
21             return false;
22         }else {
23             Person p = (Person) o;
24             if(p.name == this.name && p.location == this.location) {
25                 return true;
26             }else{
27                 return false;
28             }
29         }
30     }
31     
32     public String info() {
33         return "name: " + name + " location: " + location;
34     }
35 }
36 
37 public class Test {
38     public static void main(String args[]) {
39         Person p1 = new Person("A");
40         Person p2 = new Person("B", "shanghai");
41         Person p3 = new Person("A");
42         System.out.println("this is" + p1);
43         System.out.println(p2.equals(p1));
44         System.out.println(p3.equals(p1));
45     }
46     
47 }

 

posted on 2018-08-23 10:20  蕾拉  阅读(586)  评论(0)    收藏  举报