Open3
【Kotlin】Tips
Kotlin サーバーサイドプログラミング実践開発
第1部
基本文法
- 変数
- valは不変。varは可変
val id = 100
var name = "Takehata"
- 関数
fun 関数名(引数): 戻り値の型
- 戻り値ない場合は省略可能
fun countLength(str: String): Int {
return str.length
}
fun displayMessage(message: String) {
println(message)
}
- 分岐(if, when)
- when: Goで言うところのswitchぶん
when (num) {
100 -> {
println("Equal to 100")
}
200 -> {
println("Equal to 200")
}
else -> {
println("Undefined value")
}
}
- 繰り返し(while, for)
- while:
while(条件式)
- for:
for (i in 1..10)
や変数名 in 開始値 until 終了値 step 増加値
や変数名 in コレクションの値
- while:
while (i < 10) {
println("i is $i")
i++
}
for (i in 1..10) {
println("i is $i")
}
for (i in 1 until 10 step 2) {
println("i is $i")
}
val list = listOf(1,2,5,6,10)
for (i in list) {
println("i is $i")
}
- クラス
- newは不要
- 継承させたいとき
- 親クラスに
open
をつけておく。関数もopen
をつけておけばオーバーライド可能 - 小クラスは、クラスの末尾に
: 親クラス
をつける- コンストラクタのプロパティ追加も可能
- 親クラスに
- 継承させたくない時
- 親クラスに
sealed
をつけることで、他ファイルから継承不可
- 親クラスに
class Human(val name: String) {...}
// ↑インスタンス化
val human = Human("Takehata")
human.showName()
open class Animal(val name: String) {
fun showName() = println("name is $name")
open fun cries() = println("")
}
// ↑継承
class Dog(name: String) : Animal(name) {
override fun cries() = println("bowwow")
}
// 継承不可
sealed class Platform {
abstract fun showName()
}
- インターフェース
- クラス同様にクラス末尾に
: インターフェース名
で実装可能
- クラス同様にクラス末尾に
interface Greeter {
fun hello()
}
// ↑
class GreeterImpl: Greeter {
override fun hello() {
println("Hello.")
}
}
- コレクション
- List, Map, Setはいずれも変更不可。
- 変更したい時は、MutableList型、MutableMap型、MutableSet型を使用する
- List
-
listOf
関数で初期化
-
- Map
-
MapOf
関数で初期化 -
mapOf(key to value)
で可能
-
- Set
-
SetOf
関数で初期化 - SetはListと違い、重複なし、順序なしのコレクション
-
- List, Map, Setはいずれも変更不可。
val intList = listOf(1, 2, 3)
val stringList = listOf("one", "two", "three")
// 変更可能
val mutableList: MutableList<Int> = mutableListOf(1, 2, 3)
mutableList.add(4)
val map: Map<Int, String> = mapOf(1 to "one", 2 to "two", 3 to "three")
val map = mapOf(1 to "one", 2 to "two", 3 to "three")
println(map.containsKey(3))
// 変更可能
val mutableMap: MutableMap<Int, String> = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
val set = setOf("one", "two", "three")
println(set.contains("three"))
// 変更可能
val mutableSet = mutableSetOf("one", "two", "three")
nullの呼び出し方
- エルビス演算子
- if(xxx==null){return}の省略形
-
?:
の左のオブジェクトがnon-nullの場合、そのオブジェクトの値が返され、nullであれば、 ?:の右側の値を返す
fun printMessageLength3(message: String?) {
message ?: return
println(message.length)
}
- 安全呼び出し
- 変数の後ろに
?
をつける
- 変数の後ろに
fun printMessageLength(message: String?) {
println(message?.length)
}
- 強制アンラップ
- 変数の後ろに
!!
をつける
- 変数の後ろに
fun printMessageLength(message: String?) {
println(message!!.length)
}
JUnit と AssertJ のサンプルコードと違いをメモ
JUnit
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class MyTest {
@Test
public void testAdd() {
int a = 1;
int b = 2;
int expected = 3;
int result = a + b;
assertEquals(expected, result);
}
}
AssertJ
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class MyTest {
@Test
public void testAdd() {
int a = 1;
int b = 2;
int expected = 3;
int result = a + b;
assertThat(result).isEqualTo(expected);
}
}
VSCode, IntelliJ IDEAショートカット対応表 自分用メモ (Mac, Windows)
動作 | キー |
---|---|
同じ単語を次々選択 | ctrl + G |