在IntelliJ Scala中引用软件包时遇到问题

时间:2016-09-01 03:43:33

标签: eclipse scala intellij-idea packages

我正在通过Martin Odersky的Scala功能编程MOOC工作。在他的Lecture 3.2中,他展示了如何使用Eclipse IDE引用存储在包中的类。但是,我正在使用IntelliJ IDE,我无法重现他的结果。我试过环顾SO和谷歌,但问题是关于高级包装相关问题。

简而言之,他有一个名为week3的软件包,在其中有2个文件。 第一个是带有内容的Rational scala类文件:

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
  }
}

第二个文件是带有下面代码的临时scala文件。他在下图中得到了结果。

object scratch {
  new week3.Rational(1,2)
}

Results

我的尝试:我尝试在IntelliJ中重现相同的结果,方法是在我的src文件夹中创建一个'week3'包,并在week3包中的类文件中包含Rational的代码。接下来,我在src文件夹中创建了一个Scala工作表,代码为:

new week3.Rational(1,2)

IDE不会标记任何错误,但右侧的交互式输出上没有显示任何内容。此外,当我尝试评估工作表时,它返回一个错误: Error ... not found

我可以帮助我找到正确的轨道吗?

1 个答案:

答案 0 :(得分:1)

检查&#34;制作项目&#34; &#34;交互模式&#34;旁边的复选框复选框。这应该解决这个问题。我自己也遇到了这个恼人的问题:)

enter image description here