var age: Int constructor(n: String, a: Int) { name = n age = a } fun getName(): String { return name } fun getAge(): Int { return age } } ू·ΕKotlin͖ʂKotlinѪձ vol1
parseName(name: String): FullName { val space = name.indexOf(' ') return FullName( name.substring(0, space), name.substring(space + 1) ) } fun main(args: Array<String>) { val name = parseName("Jane Doe") val first = name.first val last = name.last println("$first $last") if (name != parseName("Jane Doe")) { println("!!!!!!!!!!!!!!!!!!!!!") println("Equals does not work!!") println("!!!!!!!!!!!!!!!!!!!!!") } } ू·ΕKotlin͖ʂKotlinѪձ vol1
fun parseName(name: String): FullName { val space = name.indexOf(' ') return FullName( name.substring(0, space), name.substring(space + 1) ) } fun main(args: Array<String>) { val name = parseName("Jane Doe") val first = name.first val last = name.last println("$first $last") if (name != parseName("Jane Doe")) { println("!!!!!!!!!!!!!!!!!!!!!") println("Equals does not work!!") println("!!!!!!!!!!!!!!!!!!!!!") } } ू·ΕKotlin͖ʂKotlinѪձ vol1
val index = indexOf(separater) return if (index < 0) this else substring(0, index) } fun main(args: Array<String>) { println( "Jane Doe".getFirstWord() ) } ू·ΕKotlin͖ʂKotlinѪձ vol1
: Element() class Text(val text: String) : Element() fun Element.extractText(): String { return extractText(this, StringBuilder()).toString() } fun extractText(e: Element, sb: StringBuilder): StringBuilder { if (e is Text) { val text = e as Text sb.append(text.text) } else if (e is Container) { val container = e as Container for (child in container.children) { extractText(child, sb) } } else error("Unrecognised element: $e") return sb } ू·ΕKotlin͖ʂKotlinѪձ vol1
fun extractText(e: Element) { if (e is Text) { val text = e as Text sb.append(text.text) } else if (e is Container) { val container = e as Container for (child in container.children) { extractText(child) } } else error("Unrecognised element: $e") } extractText(this) return sb.toString() } ू·ΕKotlin͖ʂKotlinѪձ vol1
fun extractText(e: Element) { if (e is Text) { sb.append(e.text) } else if (e is Container) { for (child in e.children) { extractText(child) } } else error("Unrecognised element: $e") } extractText(this) return sb.toString() } ू·ΕKotlin͖ʂKotlinѪձ vol1
fun extractText(e: Element) { if (e is Text) { sb.append(e.text) } else if (e is Container) { e.children.forEach(::extractText) } else error("Unrecognised element: $e") } extractText(this) return sb.toString() } ू·ΕKotlin͖ʂKotlinѪձ vol1
: Element() class Text(val text: String) : Element() fun Element.extractText(): String { val sb = StringBuilder() fun extractText(e: Element) { when (e) { is Text -> sb.append(e.text) is Container -> e.children.forEach(::extractText) } } extractText(this) return sb.toString() } ू·ΕKotlin͖ʂKotlinѪձ vol1
c: Boolean) fun main(args: Array<String>) { val ex = Example(1, null, true) val a = ex.a val b = ex.b val c = ex.c println("a = " + a + ", b = " + b + ", c = " + c) } ू·ΕKotlin͖ʂKotlinѪձ vol1
val list = mutableListOf<String>() for (it in numbers) { if (it % 16 == 0) { list.add("0x" + it.toString(16)) } } println(list) } ू·ΕKotlin͖ʂKotlinѪձ vol1
buildSequence { var a = 1 var b = 1 while (true) { yield(a) val tmp = a a = b b += tmp } } println( seq.take(20).toList() ) } ू·ΕKotlin͖ʂKotlinѪձ vol1
buildSequence { var a = 1 var b = 1 yield(2) while (true) { yield(a) val tmp = a if (tmp > 10) continue a = b b += tmp } } println( seq.take(20).toList() ) } ू·ΕKotlin͖ʂKotlinѪձ vol1