在同一个包和目录中找不到符号 [英] Cannot find symbol in same package and directory

查看:300
本文介绍了在同一个包和目录中找不到符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个课程,包括课程和课程。它们都在同一个包中和相同的目录中。

I have two classes, Offering and Course. They are both in the same package and the same directory.

Offering.java:

Offering.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;
    }
}

Course.java:

Course.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;
    }
}

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

When I try to compile Offering, I get the errors:

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

我知道错误意味着编译器无法对课程做任何事情,但我真的不知道为什么。我也知道它最终会变得非常明显,但我似乎无法弄明白。任何帮助都会非常感激。

I know that the error means the compiler is unable to do anything with 'Course,' but I don't really know why. I also know that it will end up being something incredibly obvious, but I just can't seem to figure it out. Any help would be really appreciated.

推荐答案

将目录更改为 assignment02 。您应该可以使用

Change directories to the parent directory of assignment02. You should then be able to use

javac assignment02\Course.java assignment02\Offering.java

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

甚至

javac assignment02\*.java

编译器正在寻找当前目录中 assignment02 包中的 Course 类(所以当你在 assignment02 目录中时,它实际上是在尝试查看 assignment02 / assignment02 ,这显然不是右边)

The compiler is is looking for the Course class in the assignment02 package FROM your current directory (so when your in the assignment02 directory, it's effectively trying to look in assignment02/assignment02, which obviously isn't right)

虽然这会纠正你当前的问题,但你会得到......

While this will correct you current problem you will then get...

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

需要更正

这篇关于在同一个包和目录中找不到符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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