No parameter Method
There is also a small problem with methods Re and im: you have to add a pair of parentheses after the name to invoke them. Take a look at the following example:
Object Complexnumbers {
def main (args:array[string]) {
val C = new Complex (1.2, 3.4)
println ("Imaginary part:" + c.im ())
}
}
you might think it might be more comfortable to use these functions as variables instead of calling them as functions. In fact, we can do this in Scala by defining the parameterless function. The difference between such a function and other functions with 0 parameters is that they do not need to be bracketed after the name, so it is not necessary to use it (but undoubtedly, they are functions), so our complex class can be rewritten as follows;
class Complex (real:double, imaginary:double) {
def re = Real
def im = Imaginary
}
For more highlights, please follow: http://bbs.superwu.cn
Focus on the two-dimensional code of Superman Academy:
Scala's non-parametric approach