简体   繁体   中英

including a java class in another class (extends) error

I've written two files (Node.java and priorityQueue.java) that are in the same directory , and I'm trying to use Node inside priorityQueue by using "extends".

However, I'm getting "error: cannot find symbol" wherever "Node" appears in my priorityQueue.java file.

Node.java:

public class Node {
  private int priority;
  private String name;
  public Node(int priority, String name) {
    this.priority = priority;
    this.name = name;
  }
  // some methods ...
}

priorityQueue.java:

import java.util.ArrayList;

public class priorityQueue extends Node {
  public ArrayList<Node> pq = new ArrayList<Node>();
  // some methods ...
  public static void main(String[] args) {
    priorityQueue p = new priorityQueue();
    Node a = new Node(10, "a");
    p.enqueue(a);
    System.out.println(p.dequeue().getName());
  }
}

This is the first error message:

priorityQueue.java:4: error: cannot find symbol
    public ArrayList<Node> pq = new ArrayList<Node>();
                     ^

I'm more used to c++ and c using imports, but I've been told that extends should work without importing anything as long as the files are in the same directory. What am I missing?

I'm on windows 10, java 14.0.1, and using notepad++ as an IDE.

do you want use priority queue based on node? if it is as i expect u shouldn't extends the class Node but u will create an Node's object through the Priority Queue class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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