2 Star 6 Fork 5

AmCoder / AmCoder

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
函数与方法.md 661 Bytes
一键复制 编辑 原始数据 按行查看 历史
AmCoder 提交于 2020-05-30 10:25 . 第一次提交
  • 方法(method)

    object Test {
      def main(args: Array[String]): Unit = {
        val tm = new TestMethod
        println(tm.sum(1, 1))
      }
    }
    
    class TestMethod {
      //定义一个求和方法
      def sum(x:Int,y:Int): Int ={
        x + y
      }
    }
  • 函数(function)

    //将方法转为函数
    val tfm = tm.sum _
    println(tfm(1, 1))
    //创建一个函数
    val newFun = (x:Int,y:Int) => x + y
    println(newFun(1, 1))
  • 方法和函数的区别

    • 函数比方法更灵活
  • 重写方法

    • scala明确规定,重写一个非抽象方法使用关键字override修饰符,调用父类的方法需要使用super关键字
其他
1
https://gitee.com/AmCoder/AmCoder.git
git@gitee.com:AmCoder/AmCoder.git
AmCoder
AmCoder
AmCoder
master

搜索帮助