Ich habe es schwer, dies zu kompilieren und kann nicht recht herausfinden, warum. Es sieht so aus, als würde ich etwas mit den verschachtelten Klassen vermasseln. Aus irgendeinem Grund hat die ByManhattan-Klasse keinen Zugriff auf Node? Wenn jemand erklären könnte, warum das mit der Fehlermeldung zusammenhängt und einen Vorschlag macht, wäre das hilfreich. Vielen Dank.Verschachtelte verschachtelte Komparator-Klassen
public class Solver {
private class Node implements Comparable<Node>{
private Board board;
private Node previous;
private int moves;
private int manhattan;
private int priority;
Node(Board b, Node p) {
board = b;
previous = p;
if (previous == null)
moves = 0;
else
moves = previous.moves + 1;
manhattan = board.manhattan();
priority = moves + manhattan;
}
public int compareTo(Node that) {
return this.priority - that.priority;
}
// Why Doesn't this work???
public Comparator<Node> manhattanOrder() {
Comparator<Node> m = new ByManhattan();
return m;
}
private class ByManhattan implements Comparator<Node> {
public int compare(Node this, Node that) { // this is line 37
return this.manhattan- that.manhattan;
}
}
}
MinPQ<Node> pq;
ArrayList<Node> solution;
// find a solution to the initial board (using the A* algorithm)
public Solver(Board initial) {
.
.
.
Der Fehler Ich erhalte ist:
Solver.java:37: error: the receiver type does not match the enclosing class type
public int compare(Node this, Node that) {
^
required: Solver.Node.ByManhattan
found: Solver.Node
'this' ist ein reserviertes Wort. Schlechte Übung, um einen Parameter nach einem reservierten Wort zu benennen. Sie sollten es anders benennen. Ändern Sie den ersten Parameter für 'int compare (Node, Node)' – shockawave123
nicht 100%, aber versuchen Sie, die Node-Klasse statisch zu machen. – shockawave123
ahhh! Danke und Entschuldigung für die komplizierte Frage zu einer einfachen Lösung. Nur das "Dies" zu ändern, hat funktioniert. Kann die Frage entfernt werden? – Paul