Home  >  Article  >  Backend Development  >  Detailed process of using def statement to define methods in Python Scala

Detailed process of using def statement to define methods in Python Scala

WBOY
WBOYforward
2022-10-06 08:00:282668browse

This article brings you relevant knowledge about Python, which mainly introduces the detailed process of using def statements to define methods in Scala. Scala methods are part of a class, and a function is an object that can be assigned to a Variables, let’s take a look at them together, I hope it will be helpful to everyone.

Detailed process of using def statement to define methods in Python Scala

[Related recommendations: Python3 video tutorial]

Scala also has methods and functions like Java. A Scala method is part of a class, while a function is an object that can be assigned to a variable. In other words, functions defined in a class are methods. In Scala, functions can be defined using df statements and val statements, while methods can only be defined using def statements. Let’s explain Scala’s method.

The definition format of Scala method is as follows:

As can be seen from the above code, Scala method is composed of multiple parts, as follows.

def functionName([参数列表]):[return type]={
    function body
    return [expr]
}

·def: Scala’s keyword, and it is fixed. The definition of a method starts with the def keyword.

·functionName: The method name of the Scala method.

·([Parameter list]): [return type]: Optional parameter list of Scala method. Each parameter in the parameter list has a name, followed by a colon and parameter type.

·function body: the body of the method.

·return [expr]: The return type of the Scala method, which can be any legal Scala data type. If there is no return value, the return type is Unit.

Below, define a method add() to implement the addition and sum of two numbers. The sample code is as follows:

def add(a:Int,b:Int):Int={
    var sum:Int =0
    sun =a +b
    return sum
}

The format of Scala’s method call is as follows:

//没有使用实例的对象调用格式
functionName(参数列表)
//方法由实例的对象来调用,可以使用类似java的格式(使用”.”号)
[instance.]functionName(参数列表]

Next, in class Test, define a method addInt() to implement the addition and sum of two integers. Here, the call is made through "class name. method name (parameter list)". The sample code is as follows:

scala>:paste                                 #多行输人模式的命令
// Entering paste mode (ctrl-D to finish)
object Test{
   def addInt(a:Int,b:Int):Int={
       var sum:Int=0
       sum=a+b
       return sum
   }
}
// Exiting paste mode, now interpreting.
defined object Test
scala>Test.addInt(4,5)
res0: Int =9

How to use val statements and def statements in scala

This article introduces Regarding "how to use val statements and def statements in Scala", many people will encounter such dilemmas during the operation of actual cases. Next, let the editor lead you to learn how to deal with these situations! I hope you will read it carefully and learn something!

In Scala, use the val statement to define functions, and the def statement to define methods.

class Test{
  def m(x: Int) = x + 3
  val f = (x: Int) => x + 3}
  
2.Scala 方法声明格式如下:
def functionName ([参数列表]) : [return type]
如果你不写等于号和方法主体,那么方法会被隐式声明为抽象(abstract),包含它的类型于是也是一个抽象类型。
3.方法定义
由一个 def 关键字开始,紧接着是可选的参数列表,一个冒号 : 和方法的返回类型,一个等于号 = ,最后是方法的主体。
Scala 方法定义格式如下:
def functionName ([参数列表]) : [return type] = {
   function body  
    return [expr](默认最后一行)}
    }
 4.函数
 函数默认参数
 cala 可以为函数参数指定默认参数值,使用了默认参数,你在调用函数的过程中可以不需要传递参数,这时函数就会调用它的默认参数值,如果传递了参数,则传递值会取代默认值。实例如下:object Test {
   def main(args: Array[String]) {
        println( "返回值 : " + addInt() );
   }
   def addInt( a:Int=5, b:Int=7 ) : Int = {
      var sum:Int = 0
      sum = a + b      return sum   }}
 函数命名参数
 般情况下函数调用参数,就按照函数定义时的参数顺序一个个传递。但是我们也可以通过指定函数参数名,并且不需要按照顺序向函数传递参数,实例如下:object Test {
   def main(args: Array[String]) {
        printInt(b=5, a=7);
   }
   def printInt( a:Int, b:Int ) = {
      println("Value of a : " + a );
      println("Value of b : " + b );
   }
   }
 函数可变参数
 Scala 允许你指明函数的最后一个参数可以是重复的,即我们不需要指定函数参数的个数,可以向函数传入可变长度参数列表。
Scala 通过在参数的类型之后放一个星号来设置可变参数(可重复的参数)。例如:
object Test {
   def main(args: Array[String]) {
        printStrings("Runoob", "Scala", "Python");
   }
   def printStrings( args:String* ) = {
      var i : Int = 0;
      for( arg <- args ){
         println("Arg value[" + i + "] = " + arg );
         i = i + 1;
      }
   }}
   递归函数
   
递归函数意味着函数可以调用它本身。
以上实例使用递归函数来计算阶乘:
object Test {
   def main(args: Array[String]) {
      for (i <- 1 to 10)
         println(i + " 的阶乘为: = " + factorial(i) )
   }
   
   def factorial(n: BigInt): BigInt = {  
      if (n <= 1)
         1  
      else    
      n * factorial(n - 1)
   }}
 匿名函数
箭头左边是参数列表,右边是函数体。使用匿名函数后,我们的代码变得更简洁了。
下面的表达式就定义了一个接受一个Int类型输入参数的匿名函数:
var inc = (x:Int) => x+1
上述定义的匿名函数,其实是下面这种写法的简写:
def add2 = new Function1[Int,Int]{  
    def apply(x:Int):Int = x+1;  
}

【Related recommendations: Python3 video tutorial

The above is the detailed content of Detailed process of using def statement to define methods in Python Scala. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete