scala 方法调用

Scala方法调用 (Scala Method Invocation)

Method invocation is the legal and correct technique to call a method in Scala programming language. The methods of a class are usually accessed by using two methods.

方法调用是用Scala编程语言调用方法的合法且正确的技术。 通常使用两种方法来访问类的方法。

  1. Object Creation

    对象创建

  2. Inheritance

    遗产

We dynamically call methods of a class using the object of the class. the correct way of accessing the methods using object are:

我们使用类的对象动态调用类的方法。 使用object访问方法的正确方法是:

1)使用点运算符调用方法 (1) Invoking methods using dot operator)

While calling a method of a class from the object of the class we use the dot (.) operator. The correct and legal way of accessing the methods of a class in Scala using the dot operator is,

从类的对象调用类的方法时,我们使用点(。)运算符。 使用点运算符访问Scala中的类方法的正确合法方法是,

    object_name.method_name(parameters)

This is the most appropriate way of accessing the method of a class using an object. Else than this some ways can be used but are not ideal. These methods do not generate any error but are not correct ways to call the method. They are,

这是使用对象访问类的方法的最合适的方法。 除此以外,可以使用某些方法,但并不理想。 这些方法不会产生任何错误,但不是正确的方法。 他们是,

    object_name . method_name(parameters)
object_name. method_name(parameters)
object_name . method_name (parameters)

Example code:

示例代码:

class car{def sound(noise:String){println(noise+" goes fast")
}
}
object MyClass {def main(args: Array[String]) {var newcar = new car();
newcar.sound("Lamborghini!")
newcar . sound("Honda!")
newcar .sound("Mercedes!")
newcar . sound ("BMW!")
}
}

Output

输出量

Lamborghini! goes fast
Honda! goes fast
Mercedes! goes fast
BMW! goes fast

2)直接调用方法 (2) Invoking methods directly)

Calling a method from a class inherits the base class or in the same class. The correct and legal way to call the method is,

从类中调用方法将继承基类或同一类。 调用该方法的正确合法方法是:

    method_name(parameters)

Other than this other methods are also legal. Like,

除此之外,其他方法也是合法的。 喜欢,

    method_name (parameters)
method_name( parameters )
method_name ( parameters )

Example code:

示例代码:

object MyClass {def sound(noise:String){println(noise+" makes  noise")
}
def main(args: Array[String]) {sound("Lamborghini!")
sound ( "Honda!")
sound ( "Mercedes!" )
}
}

Output

输出量

Lamborghini! makes  noise
Honda! makes  noise
Mercedes! makes  noise

3)调用带有参数的方法 (3) Invoking methods with parameters)

When the method contains arguments and calling methods with parameters. For invoking methods with the argument we will separate the parameters using commas and single space.

当方法包含参数并使用参数调用方法时。 对于使用参数调用方法,我们将使用逗号和单个空格分隔参数。

    object_name.method_name(parameter1, parameter2)

Other legal methods that are not correct but does not compile to an error,

其他不正确但无法编译为错误的合法方法,

    object_name. method_name(parameter1, parameter2)
object_name.method_name (parameter1, parameter2)
object_name.method_name( parameter1 , parameter2 )
object_name. method_name ( parameter1 , parameter2 )

Example code:

示例代码:

class  calculator{def add(a:Int , b:Int){println("The sum of "+a+" and "+b+" is "+(a+b))
}
}
object MyClass {def main(args: Array[String]) {var calc = new calculator();
calc.add(12,34)
calc. add(1,434)
calc.add (2,3)
calc.add(15, 4)
calc. add ( 232,314 )
}
}

Output

输出量

The sum of 12 and 34 is 46
The sum of 1 and 434 is 435
The sum of 2 and 3 is 5
The sum of 15 and 4 is 19
The sum of 232 and 314 is 546

4)调用特殊方法 (4) Invoking special method)

When a method in Scala does not accept any argument. So, while invoking no arguments are passed which make the parentheses optional. Without parenthesis, the code becomes much more readable and also easy the programmers work.

当Scala中的方法不接受任何参数时。 因此,在调用时,不会传递使括号成为可选参数的参数。 没有括号,代码变得更具可读性,并且使程序员易于工作。

Both this is legal and correct ways to invoke a method that does not take any parameter.

这是调用不带任何参数的方法的合法方法和正确方法。

    object_name.method_name()
object_name.method_name

Example code:

示例代码:

class car{def sound(){println("Broom Broom Brooooom!!!!!")
}
}
object MyClass {def main(args: Array[String]) {var newcar = new car();
newcar.sound()
newcar.sound
}
}

Output

输出量

Broom Broom Brooooom!!!!!
Broom Broom Brooooom!!!!!

翻译自: https://www.includehelp.com/scala/method-invocation-in-scala.aspx

scala 方法调用

scala 方法调用_Scala中的方法调用相关推荐

  1. scala 方法重载_Scala中的方法重载

    scala 方法重载 Scala方法重载 (Scala method overloading) Method overloading is a method that is redefined in ...

  2. thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法)

    thinkphp模型中的获取器和修改器(根据字段名自动调用模型中的方法) 一.总结 记得看下面 1.获取器的作用是在获取数据的字段值后自动进行处理 2.修改器的作用是可以在数据赋值的时候自动进行转换处 ...

  3. 【Groovy】闭包 Closure ( 闭包中调用 Groovy 脚本中的方法 | owner 与 delegate 区别 | 闭包中调用对象中的方法 )

    文章目录 一.闭包中调用 Groovy 脚本中的方法 二.owner 与 delegate 区别 三.闭包中调用 Groovy 对象中的方法 一.闭包中调用 Groovy 脚本中的方法 在 Groov ...

  4. kotlin调用类中的方法_一种轻松的方法来测试Kotlin中令人沮丧的静态方法调用

    kotlin调用类中的方法 by Oleksii Fedorov 通过Oleksii Fedorov 一种轻松的方法来测试Kotlin中令人沮丧的静态方法调用 (A stress-free way t ...

  5. asp.net页面调用cs中的方法

    其中GetArea 在cs文件中的方法, GetArea(string provincecode, string citycode, string areacode) 在页面中可以这样调用cs中的方法 ...

  6. 为什要使用BindService?为了调用服务中的方法

    MainActivity类: package com.itheima74.whybindservice.service; import android.app.Service; import andr ...

  7. python类内部方法调用_python如何调用类中的方法

    调用同一个类中的方法 首先类中的方法在定义的时候需要先加参数self,例如:def SaveData(self,ip): print(ip)如果无self参数则不能在同一个类中调用(之前一直在这里犯错 ...

  8. 在form2中调用form1中的方法并改变form1中控件值的另一种方法

    在窗体编程中,经常碰到到要在form2中调用form1中的方法并改变form1中控件值,但是又不能用new form1这种方法,因为这是生成一个实例. 网上有些说法是用委托和事件,笔者尝试没有成功,可 ...

  9. 反射_通过反射调用类中指定方法、属性

    一.调用指定方法 通过反射,调用类中的方法,通过Method类完成.步骤: 1.通过Class类的getMethod(String name,Class.......parameterTypes)方法 ...

最新文章

  1. 用 Redis 搞定游戏中的实时排行榜,附源码!
  2. video视频播放以及主流浏览器兼容
  3. 计算机网络拓扑结构的选择规则,赖工为您讲解计算机网络的拓扑结构
  4. 计算机网络的组成和结构ppt,常见的计算机网络拓扑结构PPT课件.pptx
  5. ASP.NET下MVC设计模式的实现
  6. ELKstack-Elasticsearch各类安装部署方法
  7. 在dom最前面插入_JavaScript中的DOM
  8. 学完这份风控入门秘籍,再卷也不怕~
  9. 程序员因为一件衣服收获了爱情,真甜!
  10. android PPPoE拨号调试记录
  11. ajax向php传参数对数据库操作
  12. 大B与小b的区别(Bps与bps)
  13. excel小写转大写公式_英文字母大小写的转换
  14. 计算机格式化为ntfs,WinXP下怎么把U盘格式化成NTFS格式?XP下把U盘格式化成NTFS格式图文教程...
  15. 制作一个私有的docker habor仓库
  16. 二期:Combined Scorecards
  17. Taichi安装与应用
  18. 一个千万级大标的精彩角逐过程(进行中...)
  19. Mixly遥控器调节LED灯亮度
  20. c0语言 测试用例,按照 Promise/A+ 手写Promise,通过promises-aplus-tests的全部872个测试用例...

热门文章

  1. ThreadLocal和InheritableThreadLocal使用
  2. 2014 网选 5007 Post Robot(暴力或者AC_自动机(有点小题大作了))
  3. python做什么模型_主题模型初学者指南[Python]
  4. python每天定时9点执行_python每天定时运行某程序代码
  5. iec60870-5-104通讯协议编程_三菱FX编程口通讯协议1——协议解读
  6. 非常不错的一款html5【404页面】,不含js脚本可以左右摆动,原生JavaScript实现日历功能代码实例(无引用Jq)...
  7. mac 安装mysql怎么卸载不干净_CleanMyMac卸载不干净怎么办?如何彻底删除Mac上的CleanMyMac?...
  8. php 下拉菜单多选get,Jquery实现select二级联动多选下拉菜单
  9. java文件 linux_Linux执行Java文件
  10. 多学一招总没错吧?SpringBoot解决前后端分离的跨域问题