无法从实现比较器的子类中访问超类方法

时间:2017-04-28 02:57:54

标签: java subclass comparator superclass

我试图在我的比较器定义中的slopeCompare子类中使用slopeTo方法。但是我在eclipse中收到错误,告诉我将slopeTo更改为静态方法。当我使用this.slopeTo()时它也不起作用。我认为子类的重点在于它们可以使用超类的所有方法。有人可以帮我澄清我在这里不理解的东西吗?如果我滥用术语,我会道歉,但比较器是一个新话题,我对它们并不完全满意。

(这是关于udacity的princeton算法课程)

import java.util.Comparator;
import edu.princeton.cs.algs4.StdDraw;

public class Point implements Comparable<Point> {

    private final int x;     // x-coordinate of this point
    private final int y;     // y-coordinate of this point

    /**
     * Initializes a new point.
     *
     * @param  x the <em>x</em>-coordinate of the point
     * @param  y the <em>y</em>-coordinate of the point
     */
    public Point(int x, int y) {
        /* DO NOT MODIFY */
        this.x = x;
        this.y = y;
    }

    /**
     * Draws this point to standard draw.
     */
    public void draw() {
        /* DO NOT MODIFY */
        StdDraw.point(x, y);
    }

    /**
     * Draws the line segment between this point and the specified point
     * to standard draw.
     *
     * @param that the other point
     */
    public void drawTo(Point that) {
        /* DO NOT MODIFY */
        StdDraw.line(this.x, this.y, that.x, that.y);
    }

    /**
     * Returns the slope between this point and the specified point.
     * Formally, if the two points are (x0, y0) and (x1, y1), then the slope
     * is (y1 - y0) / (x1 - x0). For completeness, the slope is defined to be
     * +0.0 if the line segment connecting the two points is horizontal;
     * Double.POSITIVE_INFINITY if the line segment is vertical;
     * and Double.NEGATIVE_INFINITY if (x0, y0) and (x1, y1) are equal.
     *
     * @param  that the other point
     * @return the slope between this point and the specified point
     */
    public double slopeTo(Point that) {
        /* YOUR CODE HERE */
        if (this.compareTo(that) == 0){
            return Double.NEGATIVE_INFINITY;
        }
        double slope = (double)(that.y-this.y)/(that.x-this.x);

        return slope;
    }

    /**
     * Compares two points by y-coordinate, breaking ties by x-coordinate.
     * Formally, the invoking point (x0, y0) is less than the argument point
     * (x1, y1) if and only if either y0 < y1 or if y0 = y1 and x0 < x1.
     *
     * @param  that the other point
     * @return the value <tt>0</tt> if this point is equal to the argument
     *         point (x0 = x1 and y0 = y1);
     *         a negative integer if this point is less than the argument
     *         point; and a positive integer if this point is greater than the
     *         argument point
     */
    public int compareTo(Point that) {
        if (this.y > that.y){
            return 1;
        }
        if (this.y < that.y){
            return -1;
        }
        if ((this.y == that.y) && (this.x < that.x)){
            return -1;
        }
        if ((this.y == that.y) && (this.x > that.x)){
            return 1;
        }
        else {
            return 0;
        }

    }

    /**
     * Compares two points by the slope they make with this point.
     * The slope is defined as in the slopeTo() method.
     *
     * @return the Comparator that defines this ordering on points
     */
    public Comparator<Point> slopeOrder() {
        /* YOUR CODE HERE */

        return new SlopeCompare();

    }

    private static class SlopeCompare implements Comparator<Point> {
        public int compare(Point a, Point b){
            if (slopeTo(a) < slopeTo(b)){
                return -1;
            }
            return 0;
        }
    }


    /**
     * Returns a string representation of this point.
     * This method is provide for debugging;
     * your program should not rely on the format of the string representation.
     *
     * @return a string representation of this point
     */
    public String toString() {
        /* DO NOT MODIFY */
        return "(" + x + ", " + y + ")";
    }

    /**
     * Unit tests the Point data type.
     */
    public static void main(String[] args) {
        /* YOUR CODE HERE */
    }
}

2 个答案:

答案 0 :(得分:0)

this中的{p> this.slopeTo()表示SlopeCompare实例,内部类确实可以访问外部类的字段和方法,但它不是继承的,因此您无法访问slopeTo() this个关键字,您可以outer.this获取外部类的实例。

两个参考解决方案:

1.使用slopeTo()定义static方法:

public static double slopeTo(Point that) {

2.从成员类中删除static个关键字:

private class SlopeCompare implements Comparator<Point> {

答案 1 :(得分:0)

抱歉,我还不能发表评论。

但是,你不能在静态方法中调用非静态方法。 静态属于类,非静态属于您可以在google中查看的实例。

如果SlopeCompare应该是静态的,你需要有一个Point实例来调用slopeTo。