First write a Scala class:
class Complex(real: double, imaginary: double) {
def re = real
def im = imaginary
override def toString() = "" + re + (if (im < 0) "" else "+") + im + "i"
}
|
Compile this using scalac:
> scalac Complex.scala |
Now write our Groovy Program:
println new Complex(1.2, 3.4) |
Now run the program (assuming scala-library.jar is in the CLASSPATH):
> groovy ComplexMain |
Which produces:
1.2+3.4i |
Note that in this example it would have been just as easy to write our Complex class using Groovy as follows:
class Complex {
def re, im
Complex (double real, double imaginary) {
re = real
im = imaginary
}
String toString() { "$re" + (im<0 ? '' : '+') + im + 'i' }
}
|
but in other cases you may have some existing Scala classes you wish to reuse from Groovy.