简体   繁体   English

为什么不加载此变量?

[英]Why won't this variable load?

The Short Story 短篇小说

I've created a program, below. 我在下面创建了一个程序。 I want to import a package I created into the program. 我想将我创建的程序包导入程序。 I can't figure out why compiler doesn't recognize my address variable, whose class is located in a file under the package I created 我不知道为什么编译器无法识别我的地址变量,该地址变量的类位于我创建的包下的文件中

address, name and date all share the same syntax, but the compiler doesn't recognize address. 地址,名称和日期都使用相同的语法,但是编译器无法识别地址。 After removing address and executing the program with the remaining classes, I can successfully run my program. 删除地址并使用剩余的类执行程序后,我可以成功运行程序。 I can't do so with address, however. 我不能用地址来做。


Further Details 更多详情

I created a program that prompts a user to enter the number of employees he or she wants to create, through the command line. 我创建了一个程序,该程序提示用户通过命令行输入他或她想要创建的员工数。 A user then enters information about an employee's name, adddress and hire date and the program displays this information. 然后,用户输入有关员工姓名,地址和雇用日期的信息,程序将显示此信息。

The code below DOES NOT CONTAIN classes for name, address and date, although I've referenced objects (and then fields) within those mentioned classes. 尽管我已经在提到的这些类中引用了对象(然后是字段),但是下面的代码不包含名称,地址和日期的类。 Those classes have been saved in other files under one folder, because I'm trying to run my code as a package. 这些类已保存在一个文件夹下的其他文件中,因为我试图将我的代码作为包运行。

The folder's name is util -- hence the package name is util. 该文件夹的名称为util,因此程序包名称为util。 Each source code begins with 每个源代码都以
package util;


import util.*;

public class EmployeeA
{
    Name name;
    Date date;
    Address address;

    public EmployeeA()
    {
        name = new Name();
        date = new Date();
        address = new Address();
    }

    public static void main(String[] args)
    {
        int x = Integer.parseInt(args[0]);
        EmployeeA[] array = new EmployeeA[x];

        for(int i = 0; i < x; i++)
        {
            array[i] = new EmployeeA();
            array[i].name.name = Input.getString("Enter employee first name and last name");
            array[i].date.date = Input.getString("Enter employee hire date in MM/DD/YYYY");
            array[i].address.address = Input.getString("Enter employee address");
        }
        for(int i = 0; i < x; i++)
            System.out.println(array[i].name.name + " was hired on " + array[i].date.date + " and lives on "
                    + array[i].address.address);

    }
}



                    }

source file #1 源文件#1

package util; 

public class Address
{
   String address;
}

source file #2 源文件#2

package util;
public class Date
{
    String date ;
}

source file #3 源文件#3

package util;

public class Name
{
    String name ;
}

employeeA.java:27: error: cannot find symbol employeeA.java:27:错误:找不到符号

array [i].address.address = Input.getString("Enter employee address");
                 ^

symbol: variable address location: variable address of type Address 符号:变量地址位置:地址类型的变量地址

employeeA.java:31: error: cannot find symbol employeeA.java:31:错误:找不到符号

System.out.println (array[i].name.name + " was hired on " + array[i].date.date + " and lives on " +  array[i].address.address);
                                                                                                                     ^

symbol: variable address location: variable address of type Address 符号:变量地址位置:地址类型的变量地址

Your address field within Address has no access modifiers - therefore it is only available to classes in the same package. 您在Address address字段没有访问修饰符-因此,该字段仅可用于同一包中的类。 Your EmployeeA class is not in the same package (it has no package statement), therefore it can't see it. 您的EmployeeA类不在同一个包中(它没有package语句),因此看不到它。 You could make it public - but it would be better to make it private and add a method to access it ( getAddress() ). 可以将其设为公开-但最好将其设为私有并添加访问它的方法( getAddress() )。 See the Java tutorial for more details. 有关更多详细信息,请参见Java教程

Having said that, your existing types are pretty anaemic at the moment - and certainly there are better types for representing a date than String ... 话虽如此,您现有的类型目前还很贫乏-当然,表示日期的类型要比String更好。

it looks like you have an extra close curly bracket at the end of the EmployeeA class. 看起来您在EmployeeA类的末尾有一个额外的大括号。 Directly below the plus sign on the last line. 在最后一行的加号正下方。 I suspect that is causing it not to compile properly. 我怀疑这导致它无法正确编译。

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

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