当我有以下课程时,我得到一个例外。
object rational {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
val x = new Rational(1,2) //> java.lang.NoClassDefFoundError: Rational (wrong name: rational)
//| at java.lang.ClassLoader.defineClass1(Native Method)
//| at java.lang.ClassLoader.defineClassCond(ClassLoader.java:631)
//| at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
//| at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:14
//| 1)
//| at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
//| at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
//| at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
//| at java.security.AccessController.doPrivileged(Native Method)
//| at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
//| at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
//| at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
//| at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
//| at rational$$anonfun$main$1.apply$mcV$sp(rational.scala:3)
//| at org.scalaide.worksheet.runtime.library.WorksheetSupport$$ano
//| Output exceeds cutoff limit.
}
class Rational (x:Int, y:Int) {
def numer = x
def denom = y
def add(that:Rational) =
new Rational (
numer * that.denom + denom * that.numer, denom * that.denom
)
override def toString() = numer + "/" + denom
}
当我将对象的名称改为理性时,一切都很好
答案 0 :(得分:3)
这可能是因为Windows文件系统的类型不敏感。这也会在Java中搞砸了。大写和小写版本的已编译.class文件无法区分,并且不能同时存在于文件系统中。
object rational
同时生成rational$.class
和rational.class
。
class Rational
生成Rational.class
。这似乎被rational.class
覆盖了。当JVM尝试阅读Rational.class
时,它找到rational.class
,看到内容错误,并抱怨被误入歧途。