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

How to use vale statement and def statement in scala

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

Share

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

This article introduces the relevant knowledge of "how to use vale statement and def statement in scala". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Functions can be defined using val statements in Scala, and methods can be defined by def statements.

Class Test {def m (x: Int) = x + 3 val f = (x: Int) = > x + 3} 2.Scala method declaration format is as follows: def functionName ([parameter list]): [return type] if you do not write the equals sign and the method body, then the method will be implicitly declared as abstract (abstract), and the type that contains it is also an abstract type. 3. The method definition starts with a def keyword, followed by an optional parameter list, a colon: and the return type of the method, an equal sign =, and finally the body of the method. The definition format of the Scala method is as follows: def functionName ([parameter list]): [return type] = {function body return [expr] (default last line)}} 4. Function default parameter cala can specify the default parameter value for the function parameter, using the default parameter, you do not need to pass parameters in the process of calling the function, then the function will call its default parameter value, if the parameter is passed, the transfer value will replace the default value. An example is as follows: object Test {def main (args: Array [String]) {println ("return value:" + addInt ());} def addInt (a:Int=5, b:Int=7): Int= {var sum:Int = 0 sum = a + b return sum}} function call parameters are passed one by one according to the order of the parameters defined by the function. But we can also specify the function parameter name without passing parameters to the function sequentially, as an example: object Test {def main (args: Array [String]) {printInt (bread5, aq7);} def printInt (a:Int, b:Int) = {println ("Value of a:" + a); println ("Value of b:" + b) }} function variable parameter Scala allows you to specify that the last parameter of the function can be repeated, that is, we do not need to specify the number of function parameters, we can pass a variable length parameter list to the function. Scala sets variable parameters (repeatable parameters) by placing an asterisk after the type of the parameter. For example: object Test {def main (args: Array [String]) {printStrings ("Runoob", "Scala", "Python");} def printStrings (args:String*) = {var I: Int = 0; for (arg)

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