scala 構文2

object MyApp{

  def main(args: Array[String]): Unit = {
  // for(ジェネレータ ※変数<-データ集合)
  for (i <- 0 to 3; j <- 0 to 3 if i != j) println(s"i, $j")

  }

}

method

object MyApp{

  // method

  def sayHi: String = {
      // println("hi!")
      "hi!"
  }

  def main(args: Array[String]): Unit = {
    println(sayHi)

  }

}

メソッドの引数

object MyApp{

  def sayHi(name: String = "yokota", age: Int = 23): Unit = {
      println(s"hi! $name ($age)")
  }

  def main(args: Array[String]): Unit = {
    sayHi("bob", 35)
    sayHi("tom", 43)
    sayHi()
    sayHi(age = 18, name="jack")

  }

}

class

class User {
  val name = "my name"
  def sayHi() =  println("hi!")
}

object MyApp{
  def main(args: Array[String]): Unit = {
    val user = new User
    println(user.name)
    user.sayHi()
  }

}

コンストラクタ引数

class User(_name: String) {
  val name = _name
  def sayHi() =  println("hi!" + this.name)
}

object MyApp{
  def main(args: Array[String]): Unit = {
    val tom = new User("tom")
    println(tom.name)
    tom.sayHi()
  }

}

継承、override

class User(val name: String) {
  def sayHi() =  println("hi!" + this.name)
}

class AdminUser(name: String, val age: Int) extends User(name) {
  def sayHello() =  println("hello!" + name + "(" + age  + ")")
  override def sayHi() = println("[admin] hi " + name)
}

object MyApp{
  def main(args: Array[String]): Unit = {
    val bob = new AdminUser("bob", 23)
    println(bob.name)
    println(bob.age)
    bob.sayHi()
    bob.sayHello()
  }

}

package

package com.hpscript.model 

class User(val name: String) {
  def sayHi() =  println("hi!" + this.name)
}

class AdminUser(name: String, val age: Int) extends User(name) {
  def sayHello() =  println("hello!" + name + "(" + age  + ")")
  override def sayHi() = println("[admin] hi " + name)
}

アクセス修飾子

class User {
  protected var name = "user"
  def sayHi() =  println("hi!" + this.name)
}

class AdminUser extends User {
  override def sayHi() = println("[admin] hi " + name)
}

object MyApp{
  def main(args: Array[String]): Unit = {
    val user = new User
    val adminUser = new AdminUser
     // println(user.name)
    user.sayHi()
    adminUser.sayHi()
  }

}

object

object User {
 def getInfo() = println("User Object")
}

class User(val name: String) {
  def sayHi() =  println("hi!" + this.name)
}

object MyApp{
  def main(args: Array[String]): Unit = {
    User.getInfo()
  }

}

抽象クラス

abstract class User{
  def sayHi()
}

class Japanese extends User {
 def sayHi() = println("こんにちは")
}

class American extends User {
 def sayHi() = println("hello")
}

object MyApp{
  def main(args: Array[String]): Unit = {
   val aki = new Japanese
   val tom = new American
   aki.sayHi()
   tom.sayHi()
  }

}

trait

trait Printable {
 def print() = println("now printing ...")
}

trait Sharable {
 def share() = println("now sharing ...")
}

class User extends Printable with Sharable

object MyApp{
  def main(args: Array[String]): Unit = {
    val user  = new User
    user.print()
    user.share()
  }

}

型パラメータ

//class MyInt {
//  def getThree(i: Int): Unit = println(s"$i $i $i")
//}

class MyData[T]{
  def getThree(i: T): Unit = println(s"$i $i $i")
}

object MyApp{
  def main(args: Array[String]): Unit = {
    val i = new MyData[Int]
    i.getThree(5)
    val s = new MyData[String]
    s.getThree("hello")
  }

}