As we Know,The Java API documentation for the equals method in Object provides a list of what defines an equivalence relation between two objects:
Reflexivity:
x.equals(x)
Symmetry:
x.equals(y) if-and-only-if (iff) y.equals(x)
Transitivity:
if x.equals(y) and y.equals(z), then x.equals(z)
Consistency:
x.equals(y) returns a consistent value given consistent state
Comparison to null:
!x.equals(null)
let's look a simple example,the test code:
import junit.framework.TestCase;
public class CourseTest extends TestCase {
public void testCreate() {
Course course = new Course("CMSC", "120");
assertEquals("CMSC", course.getDepartment());
assertEquals("120", course.getNumber());
}
public void testEquality() {
Course courseA = new Course("NURS", "201");
Course courseAPrime = new Course("NURS", "201");
assertEquals(courseA, courseAPrime);
Course courseB = new Course("ARTH", "330");
assertFalse(courseA.equals(courseB));
// reflexivity
assertEquals(courseA, courseA);
// transitivity
Course courseAPrime2 = new Course("NURS", "201");
assertEquals(courseAPrime, courseAPrime2);
assertEquals(courseA, courseAPrime2);
// symmetry
assertEquals(courseAPrime, courseA);
// consistency
assertEquals(courseA, courseAPrime);
// comparison to null
assertFalse(courseA.equals(null));
assertFalse(courseA.equals("CMSC-120"));
}
}
the class is:
public class Course {
private String department;
private String number;
public Course(String department, String number) {
this.department = department;
this.number = number;
}
public String getDepartment() {
return department;
}
public String getNumber() {
return number;
}
@Override
public boolean equals(Object object) {
if (object == null)
return false;
if (!(object instanceof Course))
return false;
Course that = (Course) object;
return this.department.equals(that.department)
&& this.number.equals(that.number);
}
}
see about:<<agile java>> Lession 9,The Contract for Equality
分享到:
相关推荐
The SCJP6 Sun Certified Programmer for Java 6 Study Guide (Exam 310-065) is a comprehensive resource that covers all the essential topics required for the certification exam. From basic concepts like ...
基于渐进和重抽样二项分布的等价性检验,李杰,邓丽君,本文讨论了k(k>2)个独立二项分布比例的等价性检验问题。主要讨论了似然比,Socre和Wald统计量在MaxT,MinP和Step-down方法下的第一类错误的概�
Chapter 19 The F-test for the equality of more than two means: Analysis of variance Chapter 20 The two-dependent-samples t-test for the mean difference Part 5 Inferential statistics: Tests for ...
this is in contrast to previous work on tree extensibility which only supported the traditional set of equality and range predicates. In a single data structure, the GiST provides all the basic ...
Compares Advertisement objects for equality. Two Advertisement objects are equal if their identification numbers are equal. operator>> This method reads an Advertisement object from an input stream....
Editor to work with two strings (string concatenation, for equality of strings for equality lengths, insert the second row in a predetermined position of the first) Server application retrieves data...
##### 2.6.10 Compare integers using if statements, relational operators and equality operators 可以使用关系运算符和等号运算符来比较整数。 ```java int a = 10; int b = 20; if (a ) { System.out....
The methods are directly applicable to language implementation, to the development of logics for reasoning about programs, and to the formal verification language properties such as type safety....
(Ed.) (1984), Equality postponed: Continuing barriers to higher education in the 1980s (Report from a Policy Conference on Postsecondary Programs for the Disadvantaged, held at Wingspread, Racine, ...
Unboxed Values**: Explains the difference between primitive values and their boxed counterparts and the implications for equality checks and other operations. 7. **Silent Failures**: Lists scenarios ...
programming inequality constraints to the equality constraint ,and that make the solving process more simple. This study focus on how to use the open source Hadoop cloud computing systems to build a...
"unexpected-function-equality" 是一个专注于函数等价性比较的开源库,它旨在帮助开发者们更直观、准确地判断两个函数是否具有相同的行为,即使它们的实现可能有所不同。在JavaScript这种动态类型的编程语言中,...
```java public class Person { private String name; private Integer age; private String address; // 构造函数、getter 和 setter 省略 @Override public boolean equals(Object obj) { if (this == ...
They can be assigned, copied, moved, compared for equality or order, and so on. There are also helper methods Json::dump, to serialize a Json to a string, and Json::parse (static) to parse a std::...
Othermajor additions deal with new developments concerning the information in-equality and simultaneous and shrinkage estimation. The Notes at the end ofeach chapter now provide not only ...
(They are instances of the same type and if each of the fields in one object matches the values of the fields in the other object) Equality必须满足三个必要条件:reflexive, symmetrics, and transitive ...
Non linear equality and inequality constrained PSO(非线性等式与不等式约束PSO)利用粒子群算法求解非线性等式和不等式约束的最小值。包括matlab完整代码。
The only source of any storage location information is the sysindexes table, which keeps track of the address of the root page for every index, and the first IAM page for the index or table....