Scala's non-parametric approach

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.