scala重载无参构造方法

Scala无参数方法 (Scala parameterless method)

A method which accepts no parameters from the calling code. It also denotes that there will not be any empty parentheses. These are special types of methods in Scala that are initialized and called especially. To initialize parameterless method you need to use the name with def keyword but w/o the "( )" parenthesis. While calling the method also we will avoid using the "( )" parenthesis.

一种不从调用代码接受任何参数的方法。 它还表示不会有任何空括号。 这些是Scala中特殊的方法类型,这些方法特别进行了初始化和调用。 要初始化无参数方法,您需要使用带def关键字的名称,但不带“()”括号。 在调用该方法时,我们也将避免使用“()”括号。

Syntax:

句法:

    def method_name = code_to_be_executed

Program to illustrate parameterless method

说明无参数方法的程序

class calculator(a: Int, b: Int)
{
def add = println("a+b = " + (a+b))
def subtract = println("a-b = " + (a-b))
def product = println("a*b = "+ (a*b) )
def division = println("a/b = " + (a/b))
}
object Main
{
def main(args: Array[String])
{
val calc = new calculator(1250, 50)
println("value of a = 1250 and b =  50")
calc.add
calc.subtract
calc.product
calc.division
}
}

Output

输出量

value of a = 1250 and b =  50
a+b = 1300
a-b = 1200
a*b = 62500
a/b = 25

Explanation:

说明:

This code initializes a class calculator and defines 4 parameterless methods in it. These methods are used to add, subtract, multiply and divide values. These methods directly print the calculated values and do not accept any parameters. The object calc of the class calculator is created with values 1250 and 50. Then with this calc object, we will call all the four methods that perform operations and returns output.

此代码初始化一个类计算器,并在其中定义4个无参数的方法 。 这些方法用于加,减,乘和除值。 这些方法直接打印计算值,不接受任何参数。 将使用值125050创建类计算器的对象calc 。 然后,使用该calc对象,我们将调用执行操作并返回输出的所有四个方法。

The parameterless method is invoked without the () parenthesis. And there is an error if we call a parameterless method using the parenthesis.

参方法在不带()括号的情况下调用。 如果我们使用括号调用无参数方法 ,则会出现错误。

If the programmer by mistake puts a parenthesis at the end of the function call, the compiler would report an error stating: "Unit does not take parameters".

如果程序员错误地在函数调用的末尾加上了括号,则编译器将报告错误,指出:“单元不带参数”。

Program to show error when parameterless method is called using parentheses

程序在使用括号调用无参数方法时显示错误

class calculator(a: Int, b: Int)
{
def division = println("a/b = " + (a/b))
}
object Main
{
def main(args: Array[String])
{
val calc = new calculator(1250, 50)
println("value of a = 1250 and b =  50")
calc.division()
}
}

Output

输出量

/home/jdoodle.scala:12: error: Unit does not take parameterscalc.division()^
one error found

Explanation:

说明:

This code has the same logic as the former code. The difference is just that while calling the division parameterless method parenthesis is used. Due to this, the compiler gives an error: "Unit does not take parameters".

此代码与以前的代码具有相同的逻辑。 区别只是在调用除法无参数方法时使用了括号。 因此,编译器将给出错误:“单元不带参数”。

Bonus : parameterless method Vs method without parameter

奖励:无参数方法与无参数方法

Scala has supports multiple types of parameter passing in its methods.

Scala在其方法中支持多种类型的参数传递。

But these two methods look quite similar but have different functionalities. The parameterless method does not accept and sort of parameter in its call. But the method without parameter accepts void parameter in its call ( nothing can be treated as a parameter).

但是这两种方法看起来很相似,但是功能不同。 无参数方法在其调用中不接受参数并对其进行排序。 但是不带参数的方法在其调用中接受void参数(不能将任何内容视为参数)。

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

scala重载无参构造方法

scala重载无参构造方法_Scala中的无参数方法相关推荐

  1. java怎么无参构造方法_Java中如何在无参构造方法中调用有参构造?

    展开全部 一般正常的都是参数多的调用参数少的.有参数的调用无参数的居e68a843231313335323631343130323136353331333365643537多. 当然你要无参调用的参的 ...

  2. 通过 SpringBoot 中使用 lombok 实现自动创建JavaBean的get/set方法、全参/无参构造方法、toString()、equals()、hashCode()

    通过SpringBoot中使用lombok实现 先导入依赖 <dependency><groupId>org.projectlombok</groupId>< ...

  3. java的无参构造方法_Java有参构造方法和无参构造方法

    1.定义: 编写一个类时没有添加无参构造方法,那么编译器会自动添加无参构造方法:(如果自己添加构造函数,无论有参数或是没参数,默认构造函数都将无效) 编写时添加了有参构造方法而未添加无参构造方法,那么 ...

  4. java无参构造赋值怎么没用_Java有参构造方法和无参构造方法详解

    一:有参数构造方法 在之前我们要为一个对象赋值,先要创建好对象之后然后"对象名.属性名"或者调用属性的setter为属性赋值.但是在很多时候觉得这样做很麻烦,最好的做法是在创建对象 ...

  5. Java有参构造方法和无参构造方法详解

    一:有参数构造方法 在之前我们要为一个对象赋值,先要创建好对象之后然后"对象名.属性名"或者调用属性的setter为属性赋值.但是在很多时候觉得这样做很麻烦,最好的做法是在创建对象 ...

  6. Fragment为什么须要无参构造方法

    日前在项目代码里遇到偷懒使用重写Fragment带参构造方法来传参的做法,顿生好奇,继承android.support.v4.app.Fragment而又不写无参构造方法不是会出现lint错误编译不通 ...

  7. 有参构造方法的作用和无参构造方法的作用

    Java无参构造方法的作用 class Person{ private String name ; // 表示人的姓名 private int age ; // 表示人的年龄 public void ...

  8. IDEA try catch快捷键、 快速生成get set 方法快捷键、快速生成有参和无参构造方法快捷键

    IDEA try catch快捷键. 快速生成get set 方法快捷键.快速生成有参和无参构造方法 1. try catch快捷键 小伙伴们在写代码的过程中,有时需要抛出异常,手写太浪费时间,我们想 ...

  9. (7)无参构造方法 有参构造方法

    掌握构造方法 1.不写: 默认有一个无参构造方法,只不过隐藏起来了 2.构造方法支持重载 3.只定义了有参构造方法,默认一个无参构造方法会消失,如果无参构造方法要用到,记得写出来 4.构造方法名字与类 ...

最新文章

  1. 冰点文库下载器停止工作解决办法
  2. C#判断年份是否为闰年
  3. Jakarta Commons Logging学习笔记
  4. ArcGIS API for Silverlight地图加载众多点时,使用Clusterer解决重叠问题
  5. mysql简单聚合函数根据条件单表查询
  6. c语言运行给拦截了怎么办,[蓝桥杯][算法训练VIP]拦截导弹 (C语言代码)
  7. 腾讯-004-两个排序数组的中位数
  8. MySQL多个条件更新多个字段
  9. pyodbc 连接informix
  10. fastadmin列表中,时间显示,格式设置
  11. Android JTT 808-2011 道路运输车辆卫星定位系统终端通讯协议及数据格式
  12. 厉害了,又一个资源神器
  13. 已解决ValueError: Worksheet named ‘Sheet‘ not found
  14. 2022年高处安装、维护、拆除操作证考试题库及在线模拟考试
  15. android bitmap回收,android BitMap回收
  16. JRebel LS client not configured解决方案
  17. 基于javaweb的旅游管理系统(java+jsp+html5+bootstrap+servlet+mysql)
  18. 湘潭大学计算机考研复试题,湘潭大学信息工程学院2019考研复试程序设计练习题...
  19. 二、PyQtGragh模块安装以及上手体验
  20. python glove训练模型_NLP.TM | GloVe模型及其Python实现

热门文章

  1. java list翻转_JAVA实现两种方法反转单列表
  2. mmdetection 使用笔记 01: 安装与简单的推理demo
  3. c# u盘使用记录_U盘如何快速清除使用记录【详解】
  4. apache启动失败_请检查相关配置.√mysql5.1已启动._1、Apache启动失败,请检查相关配置-百度经验...
  5. python中垃圾回收机制_Python中的变量和垃圾回收机制
  6. html中日期格式化函数,JavaScript日期时间格式化函数分享
  7. 用python画大雄_python制作斗图生成器
  8. 华为4g模块 linux驱动程序,定制Android之4G-LTE模块驱动
  9. UVA----10082 WERTYU【字符串】
  10. Caffe学习记录(十一) ICNet分割网络学习