kotlinのlet,with,run,apply,alsoの使用

# 1 let　kotlinとjavaの比較

* java

```java
 public final class LetFunctionKt {
   public static final void main(@NotNull String[] args) {
      Intrinsics.checkParameterIsNotNull(args, "args");
      String var = "testLet";
      int var1 = var.length();
      System.out.println(var1);
      int result = 1000;
      System.out.println(result);
   }
}
```

* kotlin

```kotlin
 fun main(args: Array<String>) {
    val result = "testLet".let {
        println(it.length)
        1000 //返却値
    }
    println(result)
 }
```

# 2 also　kotlinとjavaの比較

* java

```java
   public static final void main(@NotNull String[] args) {
      String var = "testLet";
      int var1 = var.length();
      System.out.println(var);
      System.out.println(var1);
   }
```

* kotlin

```kotlin
fun main(args: Array<String>) {
    val result = "testLet".also {
        println(it.length)
        1000 //返却値ではない
    }
    println(result)
}
```

# 3 run kotlinとjavaの比較

* java

```java
   public static final void main(@NotNull String[] args) {
      User user = new User("田中", 12, "080-1111-1111");
      String var = "私の名前は " + user.getName() + ", 私は " + user.getAge() + " 才, 私の電話番号は " + user.getPhoneNum() + " です。";
      System.out.println(var);
      int result = 1000;
      String var3 = "result: " + result;
      System.out.println(var3);
   }
```

* kotlin

```kotlin
fun main(args: Array<String>) {
    val user = User("田中", 12, "080-1111-1111")

    val result = user.run {
        println("私の名前は $name, 私は $age 才, 私の電話番号は $phoneNum です。")
        1000 //返却値
    }
    println("result: $result")
}
```

# 4 apply　 kotlinとjavaの比較

* java

```java
   public static final void main(@NotNull String[] args) {
      User user = new User("田中", 12, "080-1111-1111");
      String var = "私の名前は " + user.getName() + ", 私は " + user.getAge() + " 才, 私の電話番号は " + user.getPhoneNum() + " です。";
      System.out.println(var);
      String var1 = "result: " + user;
      System.out.println(var1);
   }
```

* kotlin

```kotlin
fun main(args: Array<String>) {
    val user = User("田中", 12, "080-1111-1111")

    val result = user.apply {
        println("私の名前は $name, 私は $age 才, 私の電話番号は $phoneNum です。")
        1000
    }
    println("result: $result")
}
```

# ５ まとめ表

| function | identifier | return |
| --- | --- | --- |
| let | it | 最後の行 |
| also | it | this |
| run | this | 最後の行 |
| apply | this | this |



# 6例

### 例１:let

```kotlin
//let使用しない例
videoPlayer?.setVideoView(activity.course_video_view)
videoPlayer?.setControllerView(activity.course_video_controller_view)
videoPlayer?.setCurtainView(activity.course_video_curtain_view)
```
 
```kotlin
//let使用する例
videoPlayer?.let {
	   it.setVideoView(activity.course_video_view)
	   it.setControllerView(activity.course_video_controller_view)
	   it.setCurtainView(activity.course_video_curtain_view)
}
```

### 例2:run,apply,run

```kotlin
fun main(){
    val name : String = run {
        val sb = StringBuilder()
        return @run sb.let {
            it.append("富士山")
        }.apply {
            append("太郎")
        }.run {
            return@run toString()
        }
    }
    println("私の名前は $name")
}

```

### 例3:also,apply

```kotlin
fun main() {
    val fruits = ArrayList<String>().also{
        it.add("オレンジ")
        it.add("バナナ")
    }.also {
        it.add("スイカ")
    }.apply {
        add("みかん")
        add("イチゴ")
    }
    println(fruits.toString())
}
```

### 例4: apply

```kotlin
fun main() {
    val animals = ArrayList<String>().apply{
        add("ライオン")
        add("ぞう")
    }
    println(animals.toString())
}
```
### 例5: with,run

```kotlin
//with 例
with(webview.settings) {
    javaScriptEnabled = true
    databaseEnabled = true
}
//run 例
webview.settings.run {
    javaScriptEnabled = true
    databaseEnabled = true
}
```