Java 实现嵌套类比较器时遇到问题

Java 实现嵌套类比较器时遇到问题,java,algorithm,class,comparator,comparable,Java,Algorithm,Class,Comparator,Comparable,我正在实现一个Point类,并尝试使用一个嵌套类来实现一个比较器,它将基于两点的斜率进行比较。我在实现这种比较器时遇到困难,不知道如何在main()函数中使用它 这是我尝试编译时收到的错误消息: Point.java:20: error: non-static variable this cannot be referenced from a static context if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2

我正在实现一个Point类,并尝试使用一个嵌套类来实现一个比较器,它将基于两点的斜率进行比较。我在实现这种比较器时遇到困难,不知道如何在main()函数中使用它

这是我尝试编译时收到的错误消息:

Point.java:20: error: non-static variable this cannot be referenced from a static context
       if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2)) {
                ^
Point.java:20: error: non-static variable this cannot be referenced from a static context
       if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2)) {
                                          ^
Point.java:22: error: non-static variable this cannot be referenced from a static context
       } else if (Point.this.slopeTo(pt1) < Point.this.slopeTo(pt2)) {
                       ^
Point.java:22: error: non-static variable this cannot be referenced from a static context
       } else if (Point.this.slopeTo(pt1) < Point.this.slopeTo(pt2)) {
                                                 ^
4 errors
Point.java:20:error:non-static变量无法从静态上下文引用此变量
如果(点.本.斯洛佩托(pt1)>点.本.斯洛佩托(pt2)){
^
java:20:error:non-static变量无法从静态上下文引用此变量
如果(点.本.斯洛佩托(pt1)>点.本.斯洛佩托(pt2)){
^
java:22:error:non-static变量无法从静态上下文引用此变量
}否则如果(点.本.斯洛佩托(pt1)<点.本.斯洛佩托(pt2)){
^
java:22:error:non-static变量无法从静态上下文引用此变量
}否则如果(点.本.斯洛佩托(pt1)<点.本.斯洛佩托(pt2)){
^
4个错误
以下是我的代码:

import java.util.Comparator;
import java.lang.Comparable;
import java.util.Arrays;
import edu.princeton.cs.algs4.StdDraw;
import edu.princeton.cs.algs4.StdOut;


public class Point implements Comparable<Point> {
    private final int x;
    private final int y;

    // constructs the point (x, y)
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

    private static class bySlope implements Comparator<Point> {
        public int compare(Point pt1, Point pt2) {
            if (Point.this.slopeTo(pt1) > Point.this.slopeTo(pt2)) {
                return 1;
            } else if (Point.this.slopeTo(pt1) < Point.this.slopeTo(pt2)) {
                return -1;
            }
            return 0;
        }
    }

    // draws this point
    public void draw() {
        StdDraw.point(x, y);
    }

    // draws the line segment from this point to that point
    public void drawTo(Point that) {
        StdDraw.line(this.x, this.y, that.x, that.y);
    }

    // string representatio
    public String toString() {
        return "(" + x + ", " + y + ")";
    }

    // compare two points by y-coordinates, breaking ties by x-coordinates
    public int compareTo(Point that) {
        if (this.y > that.y) {
            return 1;
        } else if (this.y < that.y) {
            return -1;
        } else {
            if (this.x > that.x) {
                return 1;
            } else if (this.x < that.x) {
                return -1;
            } else {
                return 0;
            }
        }
    }

    // the slope between this point and that point
    public double slopeTo(Point that) {
        double lineSlope;
        // horizontal line
        if (that.y == this.y && that.x != this.x) {
            lineSlope = (1.0 - 1.0) / 1.0;
        } else if (that.y != this.y && that.x == this.x) {
            lineSlope = Double.POSITIVE_INFINITY;
        } else if (that.y == this.y && that.x == this.x) {
            lineSlope = Double.NEGATIVE_INFINITY;
        } else {
            lineSlope = (that.y - this.y) / (that.x - this.x);
        }
        return lineSlope;
    }

    // compare two points by slopes they make with this point
    public Comparator<Point> slopeOrder() {
        return new bySlope();
    }

    public static void main(String args[]) {
        Point[] myPoints = new Point[3];

        myPoints[0] = new Point(1,2);
        myPoints[1] = new Point(3,4);
        myPoints[2] = new Point(7,8);

        Arrays.sort(myPoints, new Point.bySlope());

        for (int i = 0; i < myPoints.length; i++) {
            StdOut.println(myPoints[i].toString());
        }
    }
}
import java.util.Comparator;
导入java.lang.com;
导入java.util.array;
导入edu.princeton.cs.algs4.StdDraw;
导入edu.princeton.cs.algs4.StdOut;
公共类点实现了可比性{
私人最终int x;
私人终审法院;
//构造点(x,y)
公共点(整数x,整数y){
这个.x=x;
这个。y=y;
}
私有静态类bySlope实现了Comparator{
公共整数比较(点pt1、点pt2){
如果(点.本.斯洛佩托(pt1)>点.本.斯洛佩托(pt2)){
返回1;
}否则如果(点.本.斯洛佩托(pt1)<点.本.斯洛佩托(pt2)){
返回-1;
}
返回0;
}
}
//画出这一点
公众抽签(){
标准点(x,y);
}
//从该点到该点绘制线段
公共无效提取到(指向){
StdDraw.line(this.x,this.y,that.x,that.y);
}
//字符串表示法
公共字符串toString(){
返回“(“+x+”、“+y+”)”;
}
//通过y坐标比较两个点,通过x坐标断开连接
公共int比较(指向该点){
如果(this.y>that.y){
返回1;
}else if(this.ythat.x){
返回1;
}else if(this.x
您应该在数组中提供内部类实例。排序可以从父类实例的角度比较点。为此,您不应该在main()函数中创建新实例,而应该从点实例中获取

所以,在main函数中,您应该使用如下内容:

Point pivot;
... // set up pivot point here
Arrays.sort(myPoints, pivot.slopeOrder());
您还应该从嵌套类定义中删除“static”,使其成为可以实际访问其父成员的内部类:

private class bySlope implements Comparator<Point> {
私有类bySlope实现了Comparator{

Remove
static
from
bySlope
。我试过了。然后我得到了以下错误:Point.java:89:error:non-static变量这不能从静态上下文数组引用。sort(myPoints,new Point.bySlope())^1错误然后你在反向修正它。解决方法不是使它静态,而是不以静态方式引用它。
点。这
?你不是在比较两行,你是在用一个点来比较另外两行。至少你的代码是这么说的。天哪!非常感谢你救了我的命!最后一个问题假设:将嵌套类定义为“非静态”有什么区别?我想我真的很困惑在这个上下文中静态和非静态的区别。“嵌套”类是静态的,它不能直接访问父非静态成员,“内部”类是非静态的,可以访问非静态父类成员,本质上,内部类实例持有对中父类实例的引用。这一点,但您可以在不使用它的情况下使用父类中的成员变量。例如,比较器中的“if(slopeTo(pt1)>slopeTo(pt2))”应该可以正常工作。