【发布时间】:2016-12-05 23:15:02
【问题描述】:
我正在学习 Coursera 的 Scala 课程,并且我已经实现了 follwong 类:
class Rational(x: Int, y: Int) {
def numer = x;
def denom = y;
def add(that: Rational) =
new Rational(
numer * that.denom + that.numer * denom,
denom * that.denom)
override def toString = numer + "/" + denom;
def main(args: Array[String]){
val x = new Rational(1, 2);
x.numer;
x.denom;
}
}
但是,我收到很多编译错误。 其中第一个出现在第一行:
Multiple markers at this line:
self constructor arguments cannot reference unconstructed this
constructor Rational#97120 is defined twice conflicting symbols both originated in file '/Users/octavian/workspace/lecture2/src/ex3.sc'
x is already defined as value x#97118
y is already defined as value y#97119
包含代码的文件被调用
Rational.scala
为什么会出现这个错误?
【问题讨论】:
-
主要方法需要在伴生对象中才能工作。另外,你在使用工作表吗?将代码粘贴到 REPL 时可以正常工作
标签: scala