Open3

【Kotlin】Tips

kinarikinari

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 (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と違い、重複なし、順序なしのコレクション
    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)
}
kinarikinari

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);
    }
}