Scala - 1

版权声明:本文为xiaobin原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaobin_HLJ80/article/details/46294029

注意的两点:

1. Class or Objcect

    在Scala把类和对象明确的分开了。

    Class相当于模板;Object是Class的实现。

2. main

    要测试代码必须使用main

def main(args: Array[String]) {
    ...
}

一、Class

package com.example

class MyClass {
  def add(a:Int, b:Int):Int = {
    var result = 0
    result = a + b
    result
  }
}

二、Object

import com.example.MyClass


object MyExec {
  val str = "Hello world!"
  val a1 = 3
  val b1 = 2
  
  def main(args: Array[String]) {
      val myExample = new MyClass
      val ret = myExample.add(a1, b1)
      if (ret > 4) {
        println(str)
      }
      else {
        println(ret)
      }
  }
}

猜你喜欢

转载自blog.csdn.net/xiaobin_HLJ80/article/details/46294029