简体   繁体   English

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

[英]can not access superclass method from within subclass that implements comparator

I am attempting to use the slopeTo method inside my slopeCompare subclass in my comparator definition. 我正在尝试在比较器定义的我的lopeCompare子类中使用lopeTo方法。 However I get an error in eclipse telling me to change slopeTo to a static method. 但是我在日食中遇到错误,告诉我将lopetoTo更改为静态方法。 It also does not work when I use this.slopeTo(). 当我使用this.slopeTo()时,它也不起作用。 I thought that the whole point of subclasses was that they can use all the methods of the superclass. 我认为子类的全部目的是它们可以使用超类的所有方法。 Can someone help me clarify what I am not understanding here? 有人可以帮我澄清一下我在这里不了解的内容吗? I apologize if I am misusing terminology but comparators are a new topic and I am not entirely comfortable with them. 如果我滥用术语,我很抱歉,但是比较器是一个新话题,我对它们并不完全满意。

(this is from the princeton algorithms course on udacity) (这来自关于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 */
    }
}

this in this.slopeTo() represents SlopeCompare instance, inner class can indeed access fields and methods of outer class, but it's not inherit, so you can not access slopeTo() with this keywords, you can ues outer.this to get an instance of the outer class. thisthis.slopeTo()代表SlopeCompare例如,内部类确实可以访问域和外部类的方法,但它不是继承,所以你不能访问slopeTo()this关键字,您可以正餐outer.this得到一个实例外层阶级。

Two solutions for references: 两种解决方案供参考:

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

public static double slopeTo(Point that) {

2.remove the static keywords from the member class: 2.从成员类中删除static关键字:

private class SlopeCompare implements Comparator<Point> {

Sorry I can't comment yet. 抱歉,我无法发表评论。

But yes, you cannot call non static method inside static method. 但是,是的,您不能在静态方法内调用非静态方法。 Static belongs to class and non-static belongs to the instance you can look more into that in google. 静态属于类,非静态属于实例,您可以在google中对其进行更多研究。

If SlopeCompare should be static you need to have an instance of Point to call slopeTo. 如果SlopeCompare应该是静态的,则需要有一个Point实例来调用lopeTo。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM