Scala Learning notes 7-operator overloading

Source: Internet
Author: User

There is no syntax for Scala to be an operator. The previous section also mentions that Scala's operators are actually method names, such as 1 + 2, which is actually 1.+ (2). We can treat it as an operator because of one of the features of Scala: If the parameter of the method is less than or equal to 1, then "." and parentheses are optional.

Scala's operator overloading refers to overloading +,-such symbols--unlike languages such as C, Java, or Python--we need to define how these symbols are implemented.

Here is an example of a "+" operator implementation. A complex class is defined here, and complex refers to the plural. Complex numbers have real and imaginary parts that need to be processed separately when calculating:

class Complex (Val real:int, Val imaginary:int) {  + (Operand:complex): Complex = {    new Comp Lex (real + operand.real, imaginary + operand.imaginary)  }  = {    + (if  else "+") + imaginary + "I"  New Complex (1, 2new Complex (2, 3  = c1 + c2println ("(" + c1+ ") + (" + C2 + ") =" + sum)

The result of the code execution is as follows:

A class named complex is created in the first line of the code, and a constructor with two parameters is defined.

A new complex instance is created in the + method as the result of the calculation. The real and imaginary parts of the results correspond to the sum of the real and imaginary parts of the two operands respectively. C1+C2 is actually c1.+ (C2) when calculating.

Since everything in Scala that looks like an operator is a method, some things related to operators, such as arithmetic precedence, how does Scala handle it? There is really no arithmetic priority in Scala, but it defines method precedence: the priority of the method is determined by the first character of the method name, and if there are two methods of the same priority in the expression, the method on the left has a higher priority. (Scala's set-up is interesting, solves the problem of arithmetic priorities, and might be able to bring some surprises somewhere). The following list shows the priority of the first character of the method name in Scala from low to high

|^&< >=  !: +   -*  /  % All other special characters

Let's look at an example program below. In this sample program, a multiplication method is added for the previous complex class:

classComplex (Val real:int, Val imaginary:int) {def+ (Operand:complex): Complex ={println ("Calling +")    NewComplex (real + operand.real, imaginary +operand.imaginary)} def* (Operand:complex): Complex ={println ("Calling *")    NewComplex (Real * operand.real-imaginary * operand.imaginary, Real * operand.imaginary + imaginary *operand.real)} override Def toString (): String={Real+ (if(Imaginary < 0) ""Else"+") + imaginary + "I"}}val C1=NewComplex (1, 4) Val C2=NewComplex (2,-3) Val C3=NewComplex (2, 2) println (C1+ C2 * C3)

Before calling the * () method, you need to call the + () method on the left, but because the * () method takes precedence, it executes first, looking at the result of the execution:

You can see that the * () method was executed first.

##########

Scala Learning notes 7-operator overloading

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.