Kotlin 構文2

getter, setter

class User(var name: String){
  var team = "red"
  get(){
    return field.toUpperCase()
  }

  set(value){
    if (value !=""){
      field = value
    }
  }

  fun sayHi(){
    println("hi $name")
  }
}

fun main(args: Array<String>){
  val tom = User("tom")
  println(tom.team)
  tom.sayHi()
}

class override

class AdminUser(name: String): User(name){
  fun sayHello(){
    println("hello $name")
  }
  override fun sayHi(){
    println("what $name")
  }
}
open class User(var name: String){
  open fun sayHi(){
    println("hi $name")
  }
}

fun main(args: Array<String>){
  val bob = AdminUser("bob")
  println(bob.name)
  bob.sayHi()
}

アクセス修飾子: privateとpublic

open class User(private var name: String){
  open fun sayHi(){
    println("hi $name")
  }
}

class extension

fun User.sayHello(){
  println("hello $name")
}

val User.myName: String
 get() = "I am $name"

class User(var name: String){
  open fun sayHi(){
    println("hi $name")
  }
}

fun main(args: Array<String>){
  val bob = User("bob")
  bob.sayHello()
  bob.sayHi()
  println(bob.myName)
}

抽象クラス

abstract class User {
  abstract fun sayHi()
}
class Japanese: User(){
  override fun sayHi(){
    println("こんにちは")
  }
}
class American: User(){
  override fun sayHi(){
    println("hi")
  }
}

fun main(args: Array<String>){
  val tom = American()
  val taro = Japanese()
  tom.sayHi()
  taro.sayHi()
}

interface

interface Sharable{
  // 抽象プロパティ、抽象メソッド、メソッド
  val version: Double
  fun share()
  fun getInfo(){
    println("Share I/F ($version)")
  }
}
class User: Sharable {
  override val version = 1.1
  override fun share(){
    println("Sharing..")
  }
}

fun main(args: Array<String>){
 val user = User()
 user.share()
 user.getInfo()
}

generics

class MyData<T> {
  fun getThree(x: T){
    println(x)
    println(x)
    println(x)
  }
}

fun main(args: Array<String>){
  val i = MyData<Int>()
  i.getThree(32)

  val s = MyData<String>()
  s.getThree("hello")
}

データクラス

data class Point(val x: Int, val y: Int)

fun main(args: Array<String>){
  val p1 = Point(3, 5)
  val p2 = Point(3, 5)
  val p3 = p1.copy()

  println(p1)
  println(if (p1 == p2) "same" else "not same")
}

List

fun main(args: Array<String>){
  val sales: List<Int> = listOf(20, 30, 40)
  println(sales[1])
  println(sales.size)
  for (sale in sales){
    println(sale)
  }
}

set

fun main(args: Array<String>){
  val answers: Set<Int> = setOf(3, 5, 8, 3)
  println(answers)
  println(answers.contains(3))

  val set1 = setOf(1, 3, 5, 8)
  val set2 = setOf(3, 5, 8, 9)

  println(set1.intersect(set2))
  println(set1.union(set2))
  println(set1.subtract(set2))
}

Map

fun main(args: Array<String>){
  val users: Map<String, Int> = mapOf("kobayashi" to 10, "kagawa" to 22, "tanaka" to 33)
  println(users["kobayashi"])
  println(users.size)
  println(users.keys)
  println(users.values)
  println(users.entries)
}

map, filter, foreach

fun main(args: Array<String>){
  val prices = listOf(53.2, 48.2, 32.8)
  prices 
   /*.map { n -> n + 1.08} //*/
   .map { it * 1.08 }
   .filter { it > 50 }
   .forEach { println(it) }
}

例外処理

fun div(a: Int, b: Int){
  try {
    println(a / b)
  } catch(e: ArithmeticException) {
   println(e.message)
  }
}

fun main(args: Array<String>){
  div(3, 0)
}

Nullable型

fun main(args: Array<String>){
  val s: String? = null
  /*println(s)*/

  /*if (s != null){
    println(s.length)
  } else {
    println(null)
  }*/
  println(s?.length ?: -1)
}