val nameList = listOf(“Taro”, “Hanako”) for (name in nameList) { println(name) } val names = arrayOf("Taro", "Hanako") for(i in names.indices) { println(names[i]) } while (x > 0) { x-- }
16 val name = "Taro" val name : String = “Taro” var str = "text" var str: String = "Text" val sb = StringBuilder("Hello") sb.append(" ").append("World!") print(sb) // Hello World!
getterが呼ばれる ▪ init でコンストラクタに共通処理が書ける ▪ 継承される可能性があるときはopenを付ける ➔何も付けなければfinal class ▪ interfacesやabstract classは標準でopenとなる クラス 24 class Person constructor(val firstName: String, var lastName: String) {} val person = Person(“Taro”, “Sato”)
29 interface Source<out T> { fun nextT(): T } fun demo(strs: Source<String>) { val objects: Source<Any> = strs } class Box<T>(t: T) { var item = t } val box: Box<Int> = Box<Int>(1)
sum = {x: Int, y: Int -> x + y} // sumの型は "(Int, Int) -> Int" println(sum(1,2)) // sum.invoke(1,2) でもOK var sum = 0 val ints = arrayOf(1, 2, 3) ints.filter { it > 0 }.forEach { sum += it } // ラムダ式の外側の()は省略可 println(sum)
var memberName: String? = null @Required var memberAccount: String? = null @ValidateTypeFailure // using calendar so basically unneeded but just in case var birthdate: LocalDate? = null @Required var memberStatus: CDef.MemberStatus? = null }