基本语法

    1. def 方法名(参数列表) [:返回值类型] = {
    2. 方法体
    3. }

    案例实操

    1. class Person {
    2. def sum(n1:Int, n2:Int) : Int = {
    3. n1 + n2
    4. }
    5. }
    6. object Person {
    7. def main(args: Array[String]): Unit = {
    8. val person = new Person()
    9. println(person.sum(10, 20))
    10. }
    11. }