2016-11-16 6 views
-2

私はPointクラスを実装しており、ネストされたクラスを使用してコンパレータを実装しようとしています。これは2つのポイントの傾きに基づいて比較します。私はこのコンパレータの実装に問題があり、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 

以下の私のコードです:

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

'bySlope'から' static'を削除します。 – shmosel

+0

私はそれを試みました。次に、次のエラーが発生します。Point.java:89:error:静的でない変数です。これは静的コンテキストから参照できません。 Arrays.sort(myPoints、new Point.bySlope()); ^ 1エラー –

+1

あなたはそれを後方に固定しています。解決方法は静的にするのではなく、静的に参照することです。 –

答えて

0

あなたは比較することは、Arrays.sortに内部クラスのインスタンスを提供する必要があります親クラスインスタンスのビューからのポイント。これを行うには、main()関数で新しいインスタンスを作成するのではなく、Pointインスタンスからインスタンスを取得するべきです。実際にその親メンバーにアクセスすることができ、それは内部クラスになるようにまた、あなたは、ネストされたクラス定義から「静的」削除してください

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

、::

ので、主な機能であなたはこのようなものを使用する必要があります

private class bySlope implements Comparator<Point> { 
+0

OMG!そんなに私の人生を保存していただきありがとうございます!もう1つ質問:ネストされたクラス定義を「非静的」にすることで、違いは何ですか?私はこの文脈で静的と非静的の違いについて本当に混乱していると思います。 –

+0

"ネストされた"クラスは静的であり、親の非静的メンバーに直接アクセスすることはできません。 "内部"クラスは非静的で、非静的な親クラスメンバーにアクセスできます。これは、親クラスからのメンバ変数を使用することができます。たとえば、コンパレータの "if(slopeTo(pt1)> slopeTo(pt2))"はうまくいくはずです。 –

関連する問題