在 IntelliJ Scala 中引用包时遇到问题
Trouble referencing packages in IntelliJ Scala
我正在学习 Martin Odersky 的关于 Scala 函数式编程的 MOOC。在他的 Lecture 3.2 中,他展示了如何使用 Eclipse IDE 引用存储在包中的 class。但是,我正在使用 IntelliJ IDE,但我无法重现他的结果。我试过环顾四周 SO 和 Google 但问题是关于高级包装的相关问题。
简而言之,他有一个名为week3 的包,里面有2 个文件。
第一个是 Rational scala class 文件,内容为:
package week3
class Rational(x: Int, y: Int) {
require(y != 0, "denominator must be nonzero")
def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
val g = gcd(x, y)
def numer = x / g
def denom = y / g
def this(x: Int) = this(x, 1)
def + (that: Rational): Rational = {
new Rational(
numer * that.denom + denom * that.numer,
denom * that.denom)
}
def unary_- : Rational = new Rational(-numer, denom)
def - (that: Rational) = this + -that
def < (that: Rational) = this.numer * that.numer < this.denom * that.numer
def max(that: Rational) = if (this < that) that else this
override def toString = {
numer+ "/" + denom
}
}
第二个文件是带有以下代码的 scratch scala 文件。他得到的结果如下图。
object scratch {
new week3.Rational(1,2)
}
我的尝试:我尝试通过在我的 src 文件夹中创建一个 'week3' 包并在 week3 包的 class 文件中包含 Rational 的代码来在 IntelliJ 中重现相同的结果。接下来,我使用以下代码在 src 文件夹中创建了一个 Scala 工作表:
new week3.Rational(1,2)
IDE 没有标记任何错误,但右侧的交互式输出没有显示任何内容。此外,当我尝试评估工作表时,它 returns 出现错误:
我能得到一些帮助让我走上正轨吗?
选中 "Interactive Mode" 复选框旁边的 "Make Project" 复选框。这应该可以解决这个问题。我自己也遇到过这个恼人的问题:)
我正在学习 Martin Odersky 的关于 Scala 函数式编程的 MOOC。在他的 Lecture 3.2 中,他展示了如何使用 Eclipse IDE 引用存储在包中的 class。但是,我正在使用 IntelliJ IDE,但我无法重现他的结果。我试过环顾四周 SO 和 Google 但问题是关于高级包装的相关问题。
简而言之,他有一个名为week3 的包,里面有2 个文件。 第一个是 Rational scala class 文件,内容为:
package week3
class Rational(x: Int, y: Int) {
require(y != 0, "denominator must be nonzero")
def gcd(a: Int, b: Int): Int = if (b == 0) a else gcd(b, a % b)
val g = gcd(x, y)
def numer = x / g
def denom = y / g
def this(x: Int) = this(x, 1)
def + (that: Rational): Rational = {
new Rational(
numer * that.denom + denom * that.numer,
denom * that.denom)
}
def unary_- : Rational = new Rational(-numer, denom)
def - (that: Rational) = this + -that
def < (that: Rational) = this.numer * that.numer < this.denom * that.numer
def max(that: Rational) = if (this < that) that else this
override def toString = {
numer+ "/" + denom
}
}
第二个文件是带有以下代码的 scratch scala 文件。他得到的结果如下图。
object scratch {
new week3.Rational(1,2)
}
我的尝试:我尝试通过在我的 src 文件夹中创建一个 'week3' 包并在 week3 包的 class 文件中包含 Rational 的代码来在 IntelliJ 中重现相同的结果。接下来,我使用以下代码在 src 文件夹中创建了一个 Scala 工作表:
new week3.Rational(1,2)
IDE 没有标记任何错误,但右侧的交互式输出没有显示任何内容。此外,当我尝试评估工作表时,它 returns 出现错误:
我能得到一些帮助让我走上正轨吗?
选中 "Interactive Mode" 复选框旁边的 "Make Project" 复选框。这应该可以解决这个问题。我自己也遇到过这个恼人的问题:)