简体   繁体   English

我如何在java中使用来自不同文件的类?

[英]How do i use a class from a different file in java?

Am new at Java and this is what am attempting to do; 我是Java新手,这是我想要做的事情;

I Have two files located on this folder on a windows machine; 我在Windows机器上的这个文件夹中有两个文件;

d:\programs\sims\javasim\src\com\jsim\
Person.java
Building.java

On my Building.java am making use of class Person located in file Person.java ie 在我的Building.java上使用位于文件Person.java中的类Person

package com.jsim;
ArrayList<Person> personList = new ArrayList<Person>();

Am compiling the files from this folder 我正在编译这个文件夹中的文件

d:\programs\sims\javasim\src

But when i try to compile Building.Java, the compiler tells me 但是当我尝试编译Building.Java时,编译器告诉我

d:\programs\sims\src\javac com/jsim/Building.java

com\jsim\Building.java:10: cannot find symbol
symbol  : class Person
location: class com.jsim.Building
        private ArrayList<Person>personList = new ArrayList<Person>();
                          ^

How can i make Building.java know about class Person in file Person.java? 如何让Building.java知道文件Person.java中的类Person?

Gath 迦特

You need to get your package names and imports to be consistent. 您需要使包名称和导入保持一致。 The information you posted contains two different package names, so you either need to put the classes in the same package or add an import statement. 您发布的信息包含两个不同的包名称,因此您需要将类放在同一个包中或添加import语句。

I discovered that when I am using Eclipse, I can't use -non-public- classes inside other files in the same package. 我发现当我使用Eclipse时,我不能在同一个包中的其他文件中使用-non-public- classes。 Note that I am using the default package while this happens. 请注意,当发生这种情况时,我正在使用默认包。 This behavior changes when I open both files and have two windows open in the editor. 当我打开两个文件并在编辑器中打开两个窗口时,此行为会更改。 In that case, I CAN use classes in other files. 在这种情况下,我可以在其他文件中使用类。

execute javac *.java or javac Person.java Building.java or javac Building.java Person.java to compile your classes. 执行javac *.javajavac Person.java Building.javajavac Building.java Person.java来编译你的类。

Seems like Person.java is not compiled before compiling Building.java file. 在编译Building.java文件之前,似乎没有编译Person.java。

Building needs Person's class file for compiling instead of .java file. 构建需要Person的类文件来编译而不是.java文件。

您必须导入您引用的类:

import Person;

I think the problem is indeed with this like: 我认为问题的确如此:

private ArrayList<Person>personList = new ArrayList<com.liftsim.Person>();

Since the class you are writing is in the same package as the others (com.jsim) you don't need to import anything. 由于您正在编写的类与其他类(com.jsim)位于同一个包中,因此您无需导入任何内容。 However, you have specified a totally different class name in your initialisation it seems (I'm guessing using the Eclipse autocomplete?) -- so simply remove all the imports and re-write the above line as follows: 但是,您在初始化中指定了一个完全不同的类名(我猜是使用Eclipse自动完成?) - 所以只需删除所有导入并重写上面的行,如下所示:

private ArrayList<Person>personList = new ArrayList<Person>();

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

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