Scala 3 — Book
 
 方法
 Language
 
 
 Info: JavaScript is currently disabled, code tabs will still work, but preferences will not be remembered.
Scala 方法
Scala 类、样例类、traits、枚举和对象都可以包含方法。 简单方法的语法如下所示:
def methodName(param1: Type1, param2: Type2): ReturnType =
 // the method body
 // goes here
这有一些例子:
def sum(a: Int, b: Int): Int = a + b
def concatenate(s1: String, s2: String): String = s1 + s2
您不必声明方法的返回类型,因此如果您愿意,可以像这样编写这些方法:
def sum(a: Int, b: Int) = a + b
def concatenate(s1: String, s2: String) = s1 + s2
这是你如何调用这些方法:
val x = sum(1, 2)
val y = concatenate("foo", "bar")
这是一个多行的方法:
def getStackTraceAsString(t: Throwable): String = {
 val sw = new StringWriter
 t.printStackTrace(new PrintWriter(sw))
 sw.toString
}
def getStackTraceAsString(t: Throwable): String =
 val sw = new StringWriter
 t.printStackTrace(new PrintWriter(sw))
 sw.toString
方法参数也可以具有默认值。
在此示例中,timeout 参数的默认值为 5000:
def makeConnection(url: String, timeout: Int = 5000): Unit =
 println(s"url=$url, timeout=$timeout")
由于方法声明中提供了默认的 超时 值,因此可以通过以下两种方式调用该方法:
makeConnection("https://localhost") // url=http://localhost, timeout=5000
makeConnection("https://localhost", 2500) // url=http://localhost, timeout=2500
Scala 还支持在调用方法时使用 命名参数,因此如果您愿意,也可以像这样调用该方法:
makeConnection(
 url = "https://localhost",
 timeout = 2500
)
当多个方法参数具有相同的类型时,命名参数特别有用。
乍一看,使用此方法,您可能想知道哪些参数设置为 true 或 false:
engage(true, true, true, false)
如果没有IDE的帮助,那段代码可能很难阅读,但这个代码要明显得多:
engage(
 speedIsSet = true,
 directionIsSet = true,
 picardSaidMakeItSo = true,
 turnedOffParkingBrake = false
)
扩展方法
扩展方法 允许您向封闭类添加新方法。
例如,如果要将两个名为 hello 和 aloha 的方法添加到 String 类中,请将它们声明为扩展方法:
extension (s: String)
 def hello: String = s"Hello, ${s.capitalize}!"
 def aloha: String = s"Aloha, ${s.capitalize}!"
"world".hello // "Hello, World!"
"friend".aloha // "Aloha, Friend!"
extension 关键字声明了括号内的参数将定义一个或多个扩展方法。
如此示例所示,可以在扩展方法体中使用 String 类型的参数 s。
下一个示例演示如何将 makeInt 方法添加到 String 类。
在这里,makeInt 采用一个名为 radix 的参数。
该代码不考虑可能的字符串到整数转换错误,但跳过细节,示例显示了它的工作原理:
extension (s: String)
 def makeInt(radix: Int): Int = Integer.parseInt(s, radix)
"1".makeInt(2) // Int = 1
"10".makeInt(2) // Int = 2
"100".makeInt(2) // Int = 4
参见
Scala方法可以更强大:它们可以采用类型参数和上下文参数。 它们在领域建模一节中有详细介绍。
Contributors to this page:
Contents
- 导言
 - Scala 3 特性
 - 为什么是 Scala 3 ?
 - Scala 的味道
 - Hello, World!
 - The REPL
 - 变量和数据类型
 - 控制结构
 - 领域建模
 - 方法
 - 头等函数
 - 单例对象
 - 集合
 - 上下文抽象
 - 顶层定义
 - 总结
 - 类型初探
 - 字符串插值
 - 控制结构
 - 领域建模
 - 工具
 - OOP 领域建模
 - 函数式领域建模
 - 方法
 - 方法特性
 - main 方法
 - 总结
 - 函数
 - 匿名函数
 - 函数变量
 - Eta 扩展
 - 高阶函数
 - 自定义 map 函数
 - 创建可以返回函数的方法
 - 总结
 - 打包和导入
 - Scala 集合
 - 集合类型
 - 集合方法
 - 总结
 - 函数式编程
 - 什么是函数式编程?
 - 不可变值
 - 纯函数
 - 函数是值
 - 函数式错误处理
 - 总结
 - 类型和类型系统
 - 类型推断
 - 泛型
 - 相交类型
 - 联合类型
 - 代数数据类型
 - 型变
 - 不透明类型
 - 结构化类型
 - 依赖函数类型
 - 其他类型
 - 上下文抽象
 - 扩展方法
 - Given 实例和 Using 语句
 - 上下文绑定
 - Given 导入
 - 实现类型类
 - 多元相等性
 - 隐式转换
 - 总结
 - 并发
 - Scala 工具
 - 使用 sbt 构建和测试 Scala 项目
 - worksheet
 - 与 Java 交互
 - 向 Java 开发者介绍Scala
 - Scala for JavaScript Developers
 - Scala for Python Developers
 - 下一步去哪