内容へ移動
猫型iPS細胞研究所
ユーザ用ツール
ログイン
サイト用ツール
検索
ツール
文書の表示
以前のリビジョン
バックリンク
最近の変更
メディアマネージャー
サイトマップ
ログイン
>
最近の変更
メディアマネージャー
サイトマップ
現在位置:
INDEX
»
kotlin
»
コードサンプル
トレース:
kotlin:kotlinコードサンプル
この文書は読取専用です。文書のソースを閲覧することは可能ですが、変更はできません。もし変更したい場合は管理者に連絡してください。
====== コードサンプル ====== ===== Hello World! ===== <code kotlin> fun main(){ println("hello world") } ↓ hello world </code> ===== クリックイベント ===== <code kotlin> class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val but = findViewById(R.id.button) as Button but.setOnClickListener { Toast.makeText(this, "テストメッセージです", Toast.LENGTH_SHORT).show() } } } </code> ===== ifの書き方 ===== <code> fun maxOf(a: Int, b: Int): Int { if (a > b) { return a } else { return b } } </code> ↓同じ <code> fun main() { println("max of 0 and 42 is ${maxOf(0, 42)}") } </code> ===== indices ===== <code> val list = listOf("a", "b", "c") println(list.lastIndex) println(list.indices) ↓ 2 0..2 </code> ===== for while ===== <code> fun main() { val items = listOf("apple", "banana", "kiwifruit") for (index in items.indices) { println("item at $index is ${items[index]}") } for (item in items) { println(item) } var index = 0 while (index < items.size) { println("item at $index is ${items[index]}") index++ } ↓ item at 0 is apple item at 1 is banana item at 2 is kiwifruit apple banana kiwifruit item at 0 is apple item at 1 is banana item at 2 is kiwifruit } ↓ item at 0 is apple item at 1 is banana item at 2 is kiwifruit apple banana kiwifruit </code> <code> fun describe(obj: Any): String = when (obj) { 1 -> "One" "Hello" -> "Greeting" is Long -> "Long" !is String -> "Not a string" else -> "Unknown" } fun main() { println(describe(1)) println(describe("Hello")) println(describe(1000L)) println(describe(2)) println(describe("other")) ↓ One Greeting Long Not a string Unknown } </code> ===== ラムダ式 フィルター マップコレクション ===== <code> val fruits = listOf("banana", "avocado", "apple", "kiwifruit") fruits .filter { it.startsWith("a") } .sortedBy { it } .map { it.toUpperCase() } .forEach { println(it) } ↓ APPLE AVOCADO </code> ===== Getter/Setter ===== 自動でGetter Setter が作られる <code> class Data(var param: Int){ } fun main() { val data = Data(5) println(data.param) // called getter data.param = 1 // called setter println(data.param) // called getter } ↓ 5 1 </code> Getter Setter をカスタマイズする場合 <code> class Animal(){ var name:String = "" var hello:String get(){ return name } set(value){ name="${value} Chan" } } fun main() { val cat = Animal() cat.hello = "HaNa" println(cat.name) // called getter } ↓ HaNa Chan </code> ===== list ===== [[https://qiita.com/kiririnyo/items/aee905225902d096f7c0|Kotlin の Collection まとめ ~List編~]] [[https://qiita.com/opengl-8080/items/36351dca891b6d9c9687|Kotlin のコレクション使い方メモ]] 不変のリストは listOf 可変のリストは mutableListOf 文字列の結合joinToString <code> fun joinOptions(options: Collection<String>) = options.joinToString(prefix = "[", postfix = "]") fun main() { val list = listOf("t", "e", "s", "t") println(joinOptions(list)) } ↓ [t, e, s, t] </code>
kotlin/kotlinコードサンプル.txt
· 最終更新: 2019/09/01 16:06 by
ips
ページ用ツール
文書の表示
以前のリビジョン
バックリンク
文書の先頭へ