Scala方法定义
//**
def max (x: Int, y: Int) : Int = { (花括号里面的是方法体)
If (x > y)
X
else
Y
}
// def main(args : Array[String]): Unit = {
def max(a:Int,b:Int):Int = {
if(a>b){
return a
}else{
return b
}
}
val result : Int = max(100,20)
printIn(result)
}
/**
* 1.方法定义
* 1).方法体中最后返回值可以使用return,如果使用了return,那么法体的返回值类型一定要指定
* 2).如果方法体中没有return,默认将方法体中最后一行计算的结果当做返回值返回。方法体的返回值类型可以省路,会自动推断返回值的类型
* 3). 定义方法传入的参数一定要指定类型
*4).方法的方法体如果可以一行搞定,那么方法体的“{...}”可以省略
* 5).如果定义方法时,省略了方法名称和方法体之间的“=”那么无论方法体最后一行计算的结束是什么,都会被丢弃,返回Unit
*6).def 定义方法
*/
def max(a:Int, b:Int): Unit {
if(a>b) {
a
}elsel{
b
}
}
def max(a:Int, b:Int) = if(a>b) a elsel b
printIn(max(100,20))
}