2016-11-05 60 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是無效的語法(因爲它是一個類)

正如你在默認包,你並不需要進口反正