无法实现嵌套类比较器 [英] Having trouble implementing a nested class comparator

查看:135
本文介绍了无法实现嵌套类比较器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个Point类并尝试使用一个嵌套类来实现一个比较器,它将根据两点的斜率进行比较。我有麻烦实现这样的比较器,不明白如何使用它在我的main()函数。



这是我得到的错误消息,当我尝试编译它:

  Point.java:20:error:非静态变量,不能从静态上下文引用
if(Point.this.slopeTo(pt1)> Point.this.slopeTo(pt2)){
^
Point.java:20:error:非静态变量这不能从静态上下文
if(Point.this.slopeTo(pt1)> Point.this.slopeTo(pt2)){
^
Point.java:22:error:非静态变量this不能从静态上下文引用
} else if(Point.this.slopeTo(pt1)< Point.this.slopeTo(pt2)){
^
Point.java:22:错误:非静态变量,不能从静态上下文引用
} else if(Point.this.slopeTo(pt1)< Point.this.slopeTo(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可比较< Point> {
private final int x;
private final int y;

//构造点(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;
}
}

//绘制这个点
public void draw(){
StdDraw.point(x,y);
}

//绘制从这一点到该点的线段
public void drawTo(Point that){
StdDraw.line(this.x,this .y,that.x,that.y);
}

//字符串表示
public String toString(){
return(+ x +,+ y +)
}

//通过y坐标比较两个点,通过x坐标断开关系
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;
}
}
}

//这点与该点之间的斜率
public double slopeTo(Point that){
double线斜率;
//水平线
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 rowSlope;
}

//通过这个点的斜率比较两点
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());
}
}
}


解决方案>

您应该将内部类实例提供给Arrays.sort以比较父类实例视图中的点。要做到这一点你不应该在main()函数中创建新的实例,而是从Point实例中获取它。



所以,在main函数中你应该使用这样: / p>

 点枢轴; 
... //设置枢轴点
Arrays.sort(myPoints,pivot.slopeOrder());

此外,您应该从嵌套类定义中删除static,以便它成为内部类,其父成员:

  private class bySlope implements Comparator< Point> {


I'm implementing a Point class and trying to use a nested class to implement a comparator, which will compare based on the slope of two points. I'm having trouble implementing such comparator and don't understand how to use it in my main() function.

This is the error message I got when I try to compile it:

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

The following are my codes:

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());
        }
    }
}

解决方案

You should provide inner class instance into Arrays.sort to compare points from view of parent class instance. To do it you should not create new instance in main() function, but get it from Point instance.

So, in main function you should use something like this:

Point pivot;
... // set up pivot point here
Arrays.sort(myPoints, pivot.slopeOrder());

Also you should remove "static" from nested class definition so it becomes inner class, that can actually access its parent members:

private class bySlope implements Comparator<Point> {

这篇关于无法实现嵌套类比较器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆