scala中命名参数函数_Scala中的命名参数和默认参数值

scala中命名参数函数

In this post, we are going to discuss the following two important concepts in Scala about Function(Method) or Constructor Parameters

在本文中,我们将讨论Scala中有关函数(方法)或构造函数参数的以下两个重要概念

  1. Named Parameters

    命名参数
  2. Default Parameter Values

    默认参数值

介绍 (Introduction)

Let’s assume that we are going to develop a HRMS (Human Resource Management System) module in Scala. It has the following Employee class.

假设我们将在Scala中开发HRMS(人力资源管理系统)模块。 它具有以下Employee类。

Employee.scala

员工规模

class Employee(val firstName: String, val lastName:String){    
     def fullName(delimiter:String):String = firstName + delimiter + lastName

     // Some useful code goes here
}

NOTE:- We will use this code snippet to explore this post objectives with simple examples.

注意:-我们将使用此代码段通过简单的示例来探索此发布目标。

In Scala, generally we make a Function (Method) call or Constructor call as shown below:

在Scala中,通常我们进行一个Function(方法)调用或Constructor调用,如下所示:

Constructor call In Scala

Scala中的构造函数调用

scala> var emp1 = new Employee("Ram","Posa")
 emp1: Employee = Employee@563e4951

Method call In Scala

Scala中的方法调用

scala> emp1.fullName("-")
res1: String = Ram-Posa

If a Constructor or Function has too many Parameters of same type, then client may get confused about passing parameters to them. In our example, Employee class constructor has two parameters of same Type : String. In this case, Client may get confused about FirstName and LastName order.

如果构造函数或函数具有太多相同类型的参数,则客户端可能会对将参数传递给它们感到困惑。 在我们的示例中,Employee类的构造函数具有两个相同Type的参数:String。 在这种情况下,客户可能会对名字和姓氏顺序感到困惑。

If client pass parameters in different order as shown below, we will get some unexpected results. It does not give any compilation or runtime errors but produce some unexpected results

如果客户端以不同的顺序传递参数,如下所示,我们将得到一些意外的结果。 它不会产生任何编译或运行时错误,但会产生一些意外结果

scala> var emp1 = new Employee("Posa","Ram")
 emp1: Employee = Employee@563e4951

 scala> emp1.fullName("-")
 res1: String = Posa-Ram

To solve This problem, Scala have some features like Named Parameters and Default Parameter values. We will discuss these two concepts in next sections.

为了解决此问题,Scala具有一些功能,例如命名参数和默认参数值。 我们将在下一节中讨论这两个概念。

什么是命名参数 (What is Named Parameter)

Named Parameter means when making a Function (Method) call or Constructor call, we use name of the Parameter (Argument or Variable) explicitly.

命名参数表示在进行函数(方法)调用或构造函数调用时,我们显式使用参数名称(自变量或变量)。

Constructor Call with Named Parameters Syntax:-

具有命名参数的构造函数调用语法:-

Constructor-Name(Parameter1-Name = Value1,  Parameter2-Name = Value2)

This syntax is very simple. Each Parameter value is preceded by a Parameter Name. Both Parameter Name and Parameter Value are separated by an equal (“=”) sign.

此语法非常简单。 每个参数值前面都有一个参数名称。 参数名称和参数值均以等号(=)分隔。

We use same syntax even for making a Function (Method) call with Named Parameters.

即使使用命名参数进行功能(方法)调用,我们也使用相同的语法。

Scala中的命名参数 (Named Parameters In Scala)

Before Scala 2.8, we have this problem. To solve these problems, Scala 2.8 has introduced a new Feature called “Named Parameters” to make a Function call or Constructor call easily with too many Parameters (Arguments) of the same type.

在Scala 2.8之前,我们有这个问题。 为了解决这些问题,Scala 2.8引入了一个称为“ 命名参数 ”的新功能,可以轻松地使用太多相同类型的参数(参数)进行函数调用或构造函数调用。

Example:-
Take our previous HRMS Employee component to explore this syntax.

例:-
使用我们之前的HRMS Employee组件来探索这种语法。

scala> var emp2 = new Employee(firstName="Ram", lastName="Posa")
  emp2: Employee = Employee@569e4901

  scala> emp2.fullName("-")
  res2: String = Ram-Posa

If we use Name Parameters, then we don’t need to worry about Parameters order. Even though, We make a call with different order, still we will get same results as shown below.

如果使用名称参数,则不必担心参数顺序。 即使我们以不同的顺序拨打电话,仍然会得到如下所示的相同结果。

scala> var emp3 = new Employee(lastName="Posa",f irstName="Ram")
  emp3: Employee = Employee@511e4001

  scala> emp3.fullName("-")
  res2: String = Ram-Posa

NOTE:- If we use all Named Parameters to make a Constructor or Function call, then there is no issue about the order of the parameters. We can pass them in any order as per our convenience. But if we have any Unnamed Parameters, then we should declare Unnamed Parameters first, then Named Parameters.

注意:-如果我们使用所有命名参数进行构造函数或函数调用,则参数顺序没有问题。 根据我们的方便,我们可以按任何顺序传递它们。 但是,如果我们有任何未命名的参数,那么我们应该先声明未命名的参数,然后再声明命名的参数。

Example:-
Suppose, our HRMS Employee component have few more Unnamed constructor parameter like empid as shown below:

例:-
假设我们的HRMS Employee组件还有更多的Unnamed构造函数参数,例如empid,如下所示:

class Employee( val empid:Int, val firstName: String, val lastName:String){    
     def fullName(delimiter:String):String = firstName + delimiter + lastName

     // Some useful code goes here
}

In this case, we have few options to make Employee Constructor call.

在这种情况下,我们几乎没有其他方法可以调用Employee Constructor。

  • Use Named Parameters for all Arguments of Employee Constructor and use them in any order.

    将名称参数用于Employee构造函数的所有参数,并以任何顺序使用它们。
  • Example:-

    例:-

scala> var emp4 = new Employee(lastName="Posa", firstName="Ram", empid=1001)
 emp4: Employee = Employee@50378a4
  • If we mix Named and Unnamed Parameters, then that unnamed parameter(s) should come first.

    如果我们将“命名参数”和“未命名参数”混合使用,则应首先使用该未命名参数。
  • scala> var emp5 = new Employee(1001, firstName="Ram", lastName="Posa")
     emp5: Employee = Employee@6d5620ce

    NOTE:- Unnamed Parameters are also known as “Positional Parameters” as they use positions to make Function or Constructor calls.

    注意:-未命名参数也称为“ 位置参数 ”,因为它们使用位置进行函数或构造函数调用。

    Scala中命名参数的优点 (Advantages of Named Parameters In Scala)

    In Scala, Named Parameters have the following Advantages:

    在Scala中,命名参数具有以下优点:

    • It avoids Client confusion in passing Parameters to Method or Constructor calls.

      在将参数传递给方法或构造函数调用时,它避免了客户端的混乱。
    • It improve the readability of the code.

      它提高了代码的可读性。

    NOTE:- Java doesn’t support Named Parameters. However, we can implement this approach by using Builder Design Pattern.

    注意: Java不支持命名参数。 但是,我们可以通过使用Builder设计模式来实现此方法。

    命名和未命名参数之间的区别 (Difference between Named and Unnamed Parameters)

    • Named Parameters uses Parameter Names to make Function or Constructor calls. Unnamed Parameters uses Parameter Positions to make Function or Constructor calls.

      命名参数使用参数名称进行函数或构造函数调用。 未命名参数使用参数位置进行函数或构造函数调用。
    • We can pass Named Parameters in any order. We should follow same order for Unnamed Parameters.

      我们可以以任何顺序传递命名参数。 对于未命名参数,我们应遵循相同的顺序。

    NOTE:- We can mix Named and Positional (Unnamed) Parameters to make Function or Constructor calls.

    注意:-我们可以混合使用命名和位置(未命名)参数来进行函数或构造函数调用。

    In Real-time Scala Based applications, we use Named Parameters with Default Parameters. We will discuss it in next section.

    在基于实时Scala的应用程序中,我们将命名参数与默认参数一起使用。 我们将在下一节中讨论它。

    什么是默认参数 (What is Default Parameter)

    Default Parameter means assigning some default value at Parameter definition in a Function or Constructor.

    默认参数意味着在函数或构造函数的参数定义中分配一些默认值。

    Constructor with Default Parameters Syntax:-

    具有默认参数的构造函数语法:-

    Class-Name(Parameter1-Name : Type = Default-Value1, 
                Parameter2-Name : Type = Default-Value2)

    This syntax is very simple. Each Parameter Default value is preceded by a Parameter Name and Type. Both Parameter Name and Parameter Value are separated by an equal (“=”) sign.

    此语法非常简单。 每个“参数默认值”前均带有“参数名称和类型”。 参数名称和参数值均以等号(=)分隔。

    We use same syntax even for defining a Function(Method) with Default Parameters.

    即使使用默认参数定义函数(方法),我们也使用相同的语法。

    Scala中的默认参数值 (Default Parameter Values In Scala)

    Scala 2.8 has introduced one more new Feature called “Default Parameters” to make a Function call or Constructor call easier.

    Scala 2.8引入了另一个新功能,称为“ 默认参数 ”,以简化函数调用或构造函数调用。

    Example:-

    例:-

    Employee.scala

    员工规模

    class Employee(val empid:Int, val firstName: String, val lastName:String){    
         def fullName(delimiter:String = " "):String = firstName + delimiter + lastName
    
         // Some useful code goes here
    }

    Here fullName method has a Default Parameter “delimiter” with some default value.

    在这里,fullName方法具有带有一些默认值的默认参数“定界符”。

    scala> var e1 = new Employee(1002, lastName = "Posa", firstName="Ram")
    e1: Employee = Employee@93081b6
    
    scala> e1.fullName("-")
    res1: String = Ram-Posa
    
    scala> e1.fullName()
    res3: String = Ram Posa
    
    scala>

    If we required, we can make a Function call with Parameters. Otherwise, we use default parameters.

    如果需要,我们可以使用参数进行函数调用。 否则,我们使用默认参数。

    Here we are using both Named Parameters to make a Constructor call and Default Parameters to make a Function call.

    在这里,我们同时使用命名参数进行构造函数调用和默认参数进行函数调用。

    Simple Syntax:- Parameter-Name: Type = Expression-Or-Value

    简单语法:-参数名称:类型=表达式或值

    We can also use an expression in defining Default Parameters. That expression is evaluated every time we make a call to that Function or Constructor.

    我们还可以在定义默认参数时使用表达式。 每当我们调用该函数或构造函数时,都会对该表达式进行评估。

    NOTE:- We can mix both Named & Unnamed Parameters as well as Default & Non-Default Parameters.

    注意:-我们可以混合使用命名和未命名参数以及默认和非默认参数。

    Scala中默认参数的优点 (Advantages of Default Parameters In Scala)

    In Scala, Default Parameters have the following Advantages:

    在Scala中,默认参数具有以下优点:

    • It is possible to implement Optional Parameters.

      可以实现可选参数。
    • It avoids code duplication.

      它避免了代码重复。

    NOTE:- Java doesn’t support Default Parameters concept. To support this, we need to use Overloading Concept to provide different Constructors or Methods.

    注意: Java不支持默认参数概念。 为此,我们需要使用重载概念来提供不同的构造函数或方法。

    That’s it all about Named Parameters and Default Parameter values in Scala. We will discuss some more Scala concepts in my coming posts.

    这就是有关Scala中命名参数和默认参数值的全部内容。 我们将在我的后续文章中讨论更多Scala概念。

    Please drop me a comment if you like my post or have any issues/suggestions.

    如果您喜欢我的帖子或有任何问题/建议,请给我评论。

    翻译自: https://www.journaldev.com/9060/named-parameters-and-default-parameter-in-scala

    scala中命名参数函数

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值