简体   繁体   English

如何在同一目录中引用Java类?

[英]How do I refer to Java classes in the same directory?

I have an SBT project that has a number of classes declared as java files. 我有一个SBT项目,该项目具有许多声明为Java文件的类。 Two of them are Table.java and LinHashMap.java. 其中两个是Table.java和LinHashMap.java。 They are both in the src/main/java/cs4370 directory. 它们都在src / main / java / cs4370目录中。 I want to create and refer to a LinHashMap object in the Table class. 我想在Table类中创建并引用LinHashMap对象。 I thought that since they are in the same directory I don't have to include an import statement. 我以为,由于它们位于同一目录中,因此不必包括导入语句。 Am I wrong? 我错了吗? When I compile I get the error: 编译时出现错误:

[error]   src/main/java/cs4370/Table.java:90: cannot find symbol
[error]   symbol:   class LinHashMap
[error]   location: class Table
[error]     index     = new LinHashMap <> (Comparable [].class, Comparable [].class);

What do I have to do to refer to LinHashMap objects? 我必须怎么做才能引用LinHashMap对象?

You need to declare them as being in the same package : 您需要声明它们在同一包中:

package mypackage;

Being in the same folder only implies that the compiler will know where to look for the files. 位于同一文件夹中仅意味着编译器将知道在哪里查找文件。 Being in the same package is required to not have to use fully specified class name. 不必使用完全指定的类名,就必须位于同一个程序包中。

That is actually the definition of what 'import' is used for. 这实际上是“进口”用途的定义。

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

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