在同一包和目录中找不到符号

2022-09-03 06:13:15

我有两个课程,提供和课程。它们都位于同一包和同一目录中。

产品.java:

package assignment02;

public class Offering implements Comparable<Offering> {
    private Course course;
    private int CRN;
    private int semester;

    public Offering(Course course, int CRN, int semester) {
        this.course = course;
        this.CRN = CRN;
        this.semester = semester;
    }

    public int getNumCredits() {
        return course.getNumCredits;
    }

    public int getCRN() {
        return CRN;
    }

    public int getSemester() {
        return semester;
    }

    public int compareTo(Offering other) {
        if(other == null) return - 1;
        return semester - other.semester;
    }
}

课程.java:

package assignment02;

public class Course {
    private String name;
    private String rubric;
    private String number;
    private int numCredits;

    public Course(String name, String rubric, String number, int numCredits) {
        this.name = name;
        this.rubric = rubric;
        this.number = number;
        this.numCredits = numCredits;
    }

    public String getName() {
        return name;
    }

    public String getRubric() {
        return rubric;
    }

    public String getNumber() {
        return number;
    }

    public int getNumCredits() {
        return numCredits;
    }
}

当我尝试编译产品时,我收到错误:

D:\CS 140\assignment02>javac Offering.java
Offering.java:4: error: cannot find symbol
    private Course course;
            ^
   symbol:   class Course
   location: class Offering

Offering.java:8: error: cannot find symbol
    public Offering(Course course, int CRN, int semester) {
                    ^
   symbol:   class Course
   location: class OfferingOffering.java:8: error: cannot find symbol

我知道这个错误意味着编译器无法对“Course”做任何事情,但我真的不知道为什么。我也知道这最终会变得非常明显,但我似乎无法弄清楚。任何帮助将不胜感激。


答案 1

将目录更改为 的父目录。然后,您应该能够使用assignment02

javac assignment02\Course.java assignment02\Offering.java

javac assignment02\Course.java 
javac assignment02\Offering.java

甚至

javac assignment02\*.java

编译器正在从当前目录中查找包中的类(因此,当您在目录中时,它实际上正在尝试查找 ,这显然是不对的)。Courseassignment02assignment02assignment02/assignment02

虽然这将纠正您当前的问题,但您将收到以下错误:

assignment02\Offering.java:15: cannot find symbol
symbol  : variable getNumCredits
location: class assignment02.Course
    return course.getNumCredits;
                 ^
1 error

这需要纠正。


答案 2

您还可以使用“-classpath”参数进行编译,并将其值设置为父目录,如下所示:

javac -classpath .. Offering.java

推荐