- package com.study;
- public class Dog {
- int id;
- String name;
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public boolean equals(Object obj) {
- //同一对象直接返回true;
- if (this == obj) {
- return true;
- }
- //只有当obj不为空,且为当前类时,才做对象特征比较
- if (obj != null && obj.getClass() == Dog.class) {
- Dog dog = (Dog) obj;
- if (this.getId() == dog.getId()) {
- return true;
- }
- }
- return false;
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/271120137520.html
来源: http://www.codesnippet.cn/detail/271120137520.html