0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

1 let kotlinと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
 fun main(args: Array<String>) {
    val result = "testLet".let {
        println(it.length)
        1000 //返却値
    }
    println(result)
 }

2 also kotlinと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
fun main(args: Array<String>) {
    val result = "testLet".also {
        println(it.length)
        1000 //返却値ではない
    }
    println(result)
}

3 run kotlinと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
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
   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
fun main(args: Array<String>) {
    val user = User("田中", 12, "080-1111-1111")

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

5 まとめ表

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

6例

例1:let

//let使用しない例
videoPlayer?.setVideoView(activity.course_video_view)
videoPlayer?.setControllerView(activity.course_video_controller_view)
videoPlayer?.setCurtainView(activity.course_video_curtain_view)
//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

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

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

例4: apply

fun main() {
    val animals = ArrayList<String>().apply{
        add("ライオン")
        add("ぞう")
    }
    println(animals.toString())
}

例5: with,run

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?