无法编译的例子

时间:2016-11-05 19:15:06

标签: scala

我有这堂课:

 class Rational(n:Int, d:Int) {
  require(d!=0)

  private val g = gcd(n.abs, d.abs)

  val numer = n/g
  val denom = d/g

  def this(n: Int) = this(n, 1);

  def add(that:Rational): Rational = new Rational(numer * that.denom + that.numer * denom, denom * that.denom)

  override def toString = numer+"/"+denom;

  private def gcd(a: Int, b: Int): Int = if(b==0) a else gcd(b, a % b)
}

这个测试类:

import Rational

object Test extends App {
  val x = new Rational(1/2)
  println("hello");
}

我试图使用

编译它们
scalac Test.scala Rational.scala

但是我收到以下错误:

Test.scala:3: error: '.' expected but ';' found.
object Test extends App {
^
one error found

有人可以指导我为什么不编译。这是一个基本错误

2 个答案:

答案 0 :(得分:2)

import Rational是无效的语法(因为它是一个类)

当您使用默认包时,无论如何都不需要导入

答案 1 :(得分:1)

删除import Rational

Rational既不是包也不是Scala对象

如果没有声明包或将Rational声明为对象,为什么会这样?