简体   繁体   English

Java/NetBeans 无法识别主 class 文件之外的常量

[英]Java/NetBeans does not recognize my constants outside of main class file

I'm working on a project, and trying to do some basic low level things that involve simulated headphones.我正在做一个项目,并尝试做一些涉及模拟耳机的基本低级事情。 The class requires three constants for volume, and they are "LOW", "MEDIUM", and "HIGH". class 需要三个音量常数,它们是“LOW”、“MEDIUM”和“HIGH”。 The class definition looks like this: class 定义如下所示:

public class HeadPhone {
    public
        static final int LOW = 1;
        static final int MEDIUM = 2;
        static final int HIGH = 3;
    private
        int volume;
        boolean pluggedIn;
        String headPhoneModel;
        String manufacturer;
        Color headPhoneColor; 

Fast forward to my separate test file, and I get the error: "C:\Users\keife\Documents\NetBeansProjects\HeadPhone\src\TestHeadPhone.java:18: error: cannot find symbol HeadPhone Apple = new HeadPhone (LOW, true, "Air Pods", symbol: variable LOW location: class TestHeadPhone". And literally all of my calls that use the variable name of "LOW", "MEDIUM", or "HIGH" are not recognized.快进到我单独的测试文件,我收到错误:“C:\Users\keife\Documents\NetBeansProjects\HeadPhone\src\TestHeadPhone.java:18: 错误:找不到符号 HeadPhone Apple = new HeadPhone (LOW, true ,“Air Pods”,符号:变量 LOW 位置:class TestHeadPhone”。从字面上看,我所有使用变量名称“LOW”、“MEDIUM”或“HIGH”的呼叫都无法识别。

What am I doing wrong?我究竟做错了什么? I'm sure my second public declaration is redundant.我确定我的第二次公开声明是多余的。 My default constructor recognizes the variable.我的默认构造函数识别该变量。 I am truly at a loss.我真的很茫然。 Here is my specified constructor, changeVolume() method, creation of new instances of HeadPhones, and referenced setter method just to give any and all relevant code.这是我指定的构造函数、changeVolume() 方法、HeadPhones 新实例的创建和引用的 setter 方法,只是为了提供任何和所有相关代码。

    // Specified Constructor, in the main class HeadPhone.java
public HeadPhone (int volume, boolean pluggedIn, 
        String headPhoneModel, String manufacturer,
        Color headPhoneColor) {
    this.volume = volume;
    this.pluggedIn = pluggedIn;
    this.headPhoneModel = headPhoneModel;
    this.manufacturer = manufacturer;
    this.headPhoneColor  = headPhoneColor;
}

// Setter method, explicitly directed to name like this
public void changeVolume (int volume) {
    this.volume = volume;
}
// Create specified HeadPhone, in the test file's main method, TestHeadPHone.java
        HeadPhone Apple = new HeadPhone (LOW, true, "Air Pods", 
                "Apple", Color.white);
        HeadPhone Bose = new HeadPhone (HIGH, true, "Quiet Comfort II",
                "Bose", Color.black);

// Change volume, called from main method in TestHeadPhone.java
        SkullCandy.changeVolume(LOW);
        Apple.changeVolume(MEDIUM);
        Bose.changeVolume(LOW);

First of all, you need to define LOW,MEDIUM,HIGH as public.首先,您需要将 LOW、MEDIUM、HIGH 定义为公共。 In java a statement terminates after the semicolon, so in your case only the LOW variable is public.在 java 中,语句在分号后终止,因此在您的情况下,只有 LOW 变量是公共的。 You need to define them as follows:您需要按如下方式定义它们:

public static final int LOW = 1;
public static final int MEDIUM = 2;
public static final int HIGH = 3;

Putting public on all variables is not redundant, it is required if you want all of them to be public.将所有变量都公开并不是多余的,如果您希望所有变量都公开,则需要这样做。

Now in your TestHeadPhone.java you need to import those variables first in order to use them directly like you want to.现在,在您的TestHeadPhone.java中,您需要先导入这些变量才能像您想要的那样直接使用它们。

It should like something like this, depending on your package name:它应该像这样,具体取决于您的 package 名称:

import com.package.HeadPhones.LOW;
import com.package.HeadPhones.MEDIUM;
import com.package.HeadPhones.HIGH;

This would allow you to use your public variables by directly using their names without the class name这将允许您通过直接使用其名称而不使用 class 名称来使用您的公共变量

Or或者

You could use the variables like so in your constructor call:您可以在构造函数调用中像这样使用变量:

// Create specified HeadPhone, in the test file's main method, TestHeadPHone.java
        HeadPhone Apple = new HeadPhone (HeadPhone.LOW, true, "Air Pods", 
                "Apple", Color.white);
        HeadPhone Bose = new HeadPhone (HeadPhone.HIGH, true, "Quiet Comfort II",
                "Bose", Color.black);

Advice建议

Another advice here, if you want to use values like these, consider using enums.这里的另一个建议是,如果您想使用这样的值,请考虑使用枚举。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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