我有这堂课:
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
有人可以指导我为什么不编译。这是一个基本错误
答案 0 :(得分:2)
import Rational
是无效的语法(因为它是一个类)
当您使用默认包时,无论如何都不需要导入
答案 1 :(得分:1)
删除import Rational
。
Rational
既不是包也不是Scala对象
如果没有声明包或将Rational声明为对象,为什么会这样?