Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Scala object

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the example analysis of Scala object, which is very detailed and has certain reference value. Friends who are interested must finish it!

Scala is a pure object-oriented language, so everything in Scala is an object, including numbers and functions. In this respect, Scala is very different from Java: Java distinguishes between primitive types (such as boolean and int) and reference types, and cannot manipulate the original variables of the function.

1 numbers and objects

Because numbers themselves are objects, they also have methods. In fact, the arithmetic expressions we usually use (such as the following example)

1 + 2 * 3 / x

Is made up of method calls. It is equivalent to the following expression, which we saw in the previous section.

(1). + ((2). * (3). / (x))

This also means that +, -, *, / is also a valid name in Scala.

These parentheses in the second expression are necessary because Scala's word splitter uses the longest rule for word segmentation. So he would put the following expression:

1 + (2)

It is understood as a combination of expressions 1., +, and 2. The result of this combination is due to 1. Is a valid expression item and is longer than expression item 1, expression item 1. Will be treated as 1. 0, making it a double rather than an int. The following expression prevents the parser from misunderstanding

(1) + (2)

2 function and object

A function is also an object in the Scala language, which may come as a surprise to Java programmers. So it is possible to pass functions as arguments, store them in variables, or as the return value of another function. Manipulating functions as values is the cornerstone of functional programming languages.

To explain why it is useful to operate a function as a value, let's consider a timer function. The purpose of this function is to perform certain operations at regular intervals. So what about the operation we're going to do to pass in the timer? So let's think of it as a function. This current function is most familiar to programmers who often program the user interface: register a callback function to be notified after the event occurs.

In the following program, the timer function is called oncePerSceond, which takes a callback function as an argument. The type of this function is written as () = > Unit, and they don't take any arguments or return anything. (the Unit keyword is similar to void in CAccord codes +.) The main function of the program calls the timer and passes a function that prints a sentence as a callback. In other words, the program prints a "time flies like an arrow" endlessly every second.

Object Timer {

Def oncePerSecond (callback: () = > Unit) {

While (true) {callback (); Thread sleep 1000}

}

Def timeFlies () {

Println ("time flies like an arrow...")

}

Def main (args: Array [String]) {

OncePerSecond (timeFlies)

}

}

Notice that we output the string using a predefined function println instead of the one in System.out.

2.1 Anonymous function

We can make this program easier to understand. First of all, we find that the purpose of defining the function timeFlies is to treat it as a parameter passed to oncePerSecond. In this way, it doesn't seem to be necessary to name this once-used function. In fact, we can define it when we use it. These can be implemented in Scala through anonymous functions, which, as the name implies, are unnamed functions. In the new version of the program, we will use an anonymous function instead of the original timeFlise function. The program looks like this:

Object TimerAnonymous {

Def oncePerSecond (callback: () = > Unit) {

While (true) {callback (); Thread sleep 1000}

}

Def main (args: Array [String]) {

OncePerSecond (() = >

Println ("time flies like an arrow..."))

}

}

The anonymous function in this example uses an arrow (= >) to separate its argument list from the code. Here the parameter list is empty, so we write a pair of empty parentheses on the left side of the right arrow. The content of the function body is the same as the timeFlise above.

The above is all the content of the article "sample Analysis of Scala objects". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report