Scala 的相关包导入
我在 Eclipse 中有一个多项目 Scala 工作区。我认为我对 Scala 导入包的方式缺乏了解而感到沮丧,但在花费了比我愿意承认的更多时间寻找解决方案之后,我无法弄清楚这个问题。我在一个简单的 2 个项目设置中重新创建了该问题。
项目 1:com.foo.mathematics 包含一个简单的 Vector 类
包含一个文件:
package com.foo.mathematics
class Vector2D(x : Double, y : Double) {
def length = math.sqrt(x*x + y*y)
}
项目 2:com.foo.analysis
package com.foo.analysis
import com.foo.mathematics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}
Eclipse 在导入行中显示错误,我得到的错误消息是:Object math is not a member of the包 com.foo.
在大纲视图中,我的导入声明是这样说的:
com.foo.analysis.<error: <none>>.Vector2D
我已尝试将导入更改为:
import mathematics.Vector2D
import _root_.com.foo.mathematics.Vector2D
两者都不起作用...
我错过了什么?
I have a multi-project Scala workspace in eclipse. I think I'm getting hosed by my lack of understanding of the way Scala imports packages, but after spending more time than I care to admit looking for a solution, I can't figure this one out. I have recreated the problem in a simple 2 project setup.
Project 1: com.foo.mathematics contains a simple Vector class
Contains one file:
package com.foo.mathematics
class Vector2D(x : Double, y : Double) {
def length = math.sqrt(x*x + y*y)
}
Project 2: com.foo.analysis
package com.foo.analysis
import com.foo.mathematics.Vector2D
class Frame(xAxis : Vector2D, yAxis : Vector2D) {
}
Eclipse shows an error in the import line, The error message that I get is: Object mathematics is not a member of the package com.foo.
In the outline view, my import statement says this:
com.foo.analysis.<error: <none>>.Vector2D
I have tried changing the import to:
import mathematics.Vector2D
import _root_.com.foo.mathematics.Vector2D
neither one works...
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
import com.foo.mathmatics.Vector2D
和import _root_.com.foo.mathmatics.Vector2D
都应该没问题。您很可能尚未将第一个项目添加到第二个项目的构建路径中(请参阅上下文菜单中的构建路径
>配置构建路径
),或者需要在第一个项目中进行更改后,清理第二个项目 (Project
>Build Clean
)。(此外,
mathmatics
看起来像是mathematics
的拼写错误,因此请仔细检查两个位置是否确实具有相同的名称。)相对包导入不会进入其中,他们只是意味着你可以这样写:
Both
import com.foo.mathmatics.Vector2D
andimport _root_.com.foo.mathmatics.Vector2D
should be fine. Most likely you either haven't added the first project to the build path of the second (seeBuild Path
>Configure Build Path
in the context menu), or need to clean the second project (Project
>Build Clean
) after making changes in the first project.(Also,
mathmatics
looks like a typo formathematics
, so double check that you really have the same name in both places.)Relative package imports don't come into it, they just mean you could write it this way: