2016-11-05 58 views
0

我有这个类:不能编译例如

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 

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

+1

删除'进口Rational' – pamu

回答

1

删除import Rational

Rational既不是包,也不是Scala的对象

为什么你有这样的,当你没有申报或Rational声明为标的物的包装。

2

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

正如你在默认包,你并不需要进口反正