Basic usage of scala's object

Scala does not have static methods and static fields. You can use the object syntax to achieve the same purpose.

object Accounts{

  private var lastNumber = 0
def newUniqueNumber()={lastNumber+=1;lastNumber}
  

}
object MySecond {

  def main(args:Array[Int]): Unit =
  {
    val chatter = new Network
    val myFace = new Network

    System.out.println("zzz");

    Accounts.newUniqueNumber()
  }
}
Because of the apply method, we can use Fraction(3,4) to construct a fraction instead of new Fraction(3,4)


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326127046&siteId=291194637