먼저 API 디자인에 의해 제한된다는 점을 알려주십시오. API를 변경하지 마십시오. 그러나 개인 기능을 추가 할 수 있습니다.다른 함수에서 'Comparator'를 반환하십시오.
public class Point implements Comparable<Point> {
public Point(int x, int y) // constructs the point (x, y)
public void draw() // draws this point
public void drawTo(Point that) // draws the line segment from this point to that point
public String toString() // string representation
public int compareTo(Point that) // compare two points by y-coordinates, breaking ties by x-coordinates
public double slopeTo(Point that) // the slope between this point and that point
public Comparator<Point> slopeOrder() // compare two points by slopes they make with this point
}
slopeOrder()
메서드의 비교 함수를 재정의하려고하면 문제가 발생합니다. 나는 메서드를 slopeOrder()
함수에서 호출하려고 시도했지만, API에 매개 변수가 없으므로 사용할 수 없습니다.
slopeOrder()
방법에서 Comparator<Point>
을 반환하는 몇 가지 해결책을 제안하십시오.
는 ASM처럼 보입니다.) 해당 회선 주석이 포함되어 있습니다.) – Antoniossss
비교를 요청해야합니까? 포인트가 비슷하면 비교기가 필요한 이유는 무엇입니까? 반환해야하는 비교기가 다른 순서를 정의해야하는 것처럼 보입니다. –
그것은 coursera (algs4)의 과제 중 하나인데, 논리 부분을 못 박았지만 Comparator 인터페이스를 다루는 것은 이번이 처음입니다. – Shanky