use of com.forstudy.efjava.ch03.item10.Point in project study-effective-java by doyoung0205.
the class Item10Test method transitivity2.
@Test
@DisplayName("equals - 컴포지션 사용")
void transitivity2() {
// given
Point p1 = new Point(1, 2);
ColorPoint p2 = new ColorPoint(1, 2, Color.ORANGE);
ColorPointComposition p3 = new ColorPointComposition(1, 2, Color.ORANGE);
// when // then
assertTrue(p1.equals(p2));
assertTrue(p1.equals(p3.asPoint()));
assertTrue(p2.equals(p3.asPoint()));
}
use of com.forstudy.efjava.ch03.item10.Point in project for-study by godndas2.
the class ColorPoint method main.
// // 코드 10-3 잘못된 코드 - 추이성 위배! (57쪽)
// @Override public boolean equals(Object o) {
// if (!(o instanceof Point))
// return false;
//
// // o가 일반 Point면 색상을 무시하고 비교한다.
// if (!(o instanceof ColorPoint))
// return o.equals(this);
//
// // o가 ColorPoint면 색상까지 비교한다.
// return super.equals(o) && ((ColorPoint) o).color == color;
// }
public static void main(String[] args) {
// 첫 번째 equals 메서드(코드 10-2)는 대칭성을 위배한다. (57쪽)
Point p = new Point(1, 2);
ColorPoint cp = new ColorPoint(1, 2, Color.RED);
System.out.println(p.equals(cp) + " " + cp.equals(p));
// 두 번째 equals 메서드(코드 10-3)는 추이성을 위배한다. (57쪽)
ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);
System.out.printf("%s %s %s%n", p1.equals(p2), p2.equals(p3), p1.equals(p3));
}
use of com.forstudy.efjava.ch03.item10.Point in project effective-java by whiteship.
the class ColorPoint method main.
public static void main(String[] args) {
// 첫 번째 equals 메서드(코드 10-2)는 대칭성을 위배한다. (57쪽)
// Point p = new Point(1, 2);
// ColorPoint cp = new ColorPoint(1, 2, Color.RED);
// System.out.println(p.equals(cp) + " " + cp.equals(p));
// 두 번째 equals 메서드(코드 10-3)는 추이성을 위배한다. (57쪽)
ColorPoint p1 = new ColorPoint(1, 2, Color.RED);
Point p2 = new Point(1, 2);
ColorPoint p3 = new ColorPoint(1, 2, Color.BLUE);
System.out.printf("%s %s %s%n", p1.equals(p2), p2.equals(p3), p1.equals(p3));
}
use of com.forstudy.efjava.ch03.item10.Point in project Lectures by FER-OOP.
the class MainList method main.
public static void main(String[] args) {
List<Point> list = new ArrayList<>();
list.add(new Point(-5, 12));
list.add(new Point(3, -4));
list.add(new Point(12, 9));
list.add(new Point(3, 4));
list.add(new Point(4, 3));
list.add(new Point(-9, 12));
list.add(new Point(-5, -12));
System.out.println("Original list:\n" + list);
Comparator<Point> comp = new PointComparator();
list.sort(comp);
System.out.println("Sorted by distance:\n" + list);
list.sort(Comparator.naturalOrder());
System.out.println("Sorted by natural order:\n" + list);
}
use of com.forstudy.efjava.ch03.item10.Point in project Lectures by FER-OOP.
the class MainTreeSetCompositeComparator method addToSet.
private static void addToSet(Set<Point> set) {
set.add(new Point(-5, 12));
set.add(new Point(3, -4));
set.add(new Point(12, 9));
set.add(new Point(3, 4));
set.add(new Point(4, 3));
set.add(new Point(-9, 12));
set.add(new Point(-5, -12));
}
Aggregations