本文主要是介绍【kotlin】let with run apply also,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
新学kotlin,记录一些函数的例子,直接上具体使用,看demo就好。
转自:
文章目录
- let
- with
- run
- apply
- also
let
kotlin、java代码对比:
//kotlinfun main(args: Array<String>) {val result = "testLet".let {println(it.length)1000}println(result)}//javapublic final class LetFunctionKt {public static final void main(@NotNull String[] args) {Intrinsics.checkParameterIsNotNull(args, "args");String var2 = "testLet";int var4 = var2.length();System.out.println(var4);int result = 1000;System.out.println(result);}
}
使用kotlin前后代码对比:
mVideoPlayer?.setVideoView(activity.course_video_view)mVideoPlayer?.setControllerView(activity.course_video_controller_view)mVideoPlayer?.setCurtainView(activity.course_video_curtain_view)mVideoPlayer?.let {it.setVideoView(activity.course_video_view)it.setControllerView(activity.course_video_controller_view)it.setCurtainView(activity.course_video_curtain_view)
}
with
kotlin、java代码对比:
//kotlinfun main(args: Array<String>) {val user = User("Kotlin", 1, "1111111")val result = with(user) {println("my name is $name, I am $age years old, my phone number is $phoneNum")1000}println("result: $result")
}//javapublic static final void main(@NotNull String[] args) {Intrinsics.checkParameterIsNotNull(args, "args");User user = new User("Kotlin", 1, "1111111");String var4 = "my name is " + user.getName() + ", I am " + user.getAge() + " years old, my phone number is " + user.getPhoneNum();System.out.println(var4);int result = 1000;String var3 = "result: " + result;System.out.println(var3);}
使用kotlin前后代码对比:
@Override
public void onBindViewHolder(ViewHolder holder, int position) {ArticleSnippet item = getItem(position);if (item == null) {return;}holder.tvNewsTitle.setText(StringUtils.trimToEmpty(item.titleEn));holder.tvNewsSummary.setText(StringUtils.trimToEmpty(item.summary));String gradeInfo = "难度:" + item.gradeInfo;String wordCount = "单词数:" + item.length;String reviewNum = "读后感:" + item.numReviews;String extraInfo = gradeInfo + " | " + wordCount + " | " + reviewNum;holder.tvExtraInfo.setText(extraInfo);...
}override fun onBindViewHolder(holder: ViewHolder, position: Int){val item = getItem(position)?: returnwith(item){holder.tvNewsTitle.text = StringUtils.trimToEmpty(titleEn)holder.tvNewsSummary.text = StringUtils.trimToEmpty(summary)holder.tvExtraInf.text = "难度:$gradeInfo | 单词数:$length | 读后感: $numReviews"... }}
run
run函数实际上可以说是let和with两个函数的结合体。
kotlin、java代码对比:
//kotlinfun main(args: Array<String>) {val user = User("Kotlin", 1, "1111111")val result = user.run {println("my name is $name, I am $age years old, my phone number is $phoneNum")1000}println("result: $result")
}//javapublic static final void main(@NotNull String[] args) {Intrinsics.checkParameterIsNotNull(args, "args");User user = new User("Kotlin", 1, "1111111");String var5 = "my name is " + user.getName() + ", I am " + user.getAge() + " years old, my phone number is " + user.getPhoneNum();System.out.println(var5);int result = 1000;String var3 = "result: " + result;System.out.println(var3);}
run函数使用前后的对比
override fun onBindViewHolder(holder: ViewHolder, position: Int){val item = getItem(position)?: returnwith(item){holder.tvNewsTitle.text = StringUtils.trimToEmpty(titleEn)holder.tvNewsSummary.text = StringUtils.trimToEmpty(summary)holder.tvExtraInf = "难度:$gradeInfo | 单词数:$length | 读后感: $numReviews"... }}override fun onBindViewHolder(holder: ViewHolder, position: Int){getItem(position)?.run{holder.tvNewsTitle.text = StringUtils.trimToEmpty(titleEn)holder.tvNewsSummary.text = StringUtils.trimToEmpty(summary)holder.tvExtraInf = "难度:$gradeInfo | 单词数:$length | 读后感: $numReviews"... }}
apply
整体作用功能和run函数很像,唯一不同点就是它返回的值是对象本身,而run函数是一个闭包形式返回,返回的是最后一行的值。
使用前后对比
mSheetDialogView = View.inflate(activity, R.layout.biz_exam_plan_layout_sheet_inner, null)mSheetDialogView.course_comment_tv_label.paint.isFakeBoldText = truemSheetDialogView.course_comment_tv_score.paint.isFakeBoldText = truemSheetDialogView.course_comment_tv_cancel.paint.isFakeBoldText = truemSheetDialogView.course_comment_tv_confirm.paint.isFakeBoldText = truemSheetDialogView.course_comment_seek_bar.max = 10mSheetDialogView.course_comment_seek_bar.progress = 0mSheetDialogView = View.inflate(activity, R.layout.biz_exam_plan_layout_sheet_inner, null).apply{course_comment_tv_label.paint.isFakeBoldText = truecourse_comment_tv_score.paint.isFakeBoldText = truecourse_comment_tv_cancel.paint.isFakeBoldText = truecourse_comment_tv_confirm.paint.isFakeBoldText = truecourse_comment_seek_bar.max = 10course_comment_seek_bar.progress = 0}
多层判空问题
mSectionMetaData?.apply{//mSectionMetaData不为空的时候操作mSectionMetaData}?.questionnaire?.apply{//questionnaire不为空的时候操作questionnaire}?.section?.apply{//section不为空的时候操作section}?.sectionArticle?.apply{//sectionArticle不为空的时候操作sectionArticle}
also
also函数和let很像,只是唯一的不同点就是let函数最后的返回值是最后一行的返回值而also函数的返回值是返回当前的这个对象。一般可用于多个扩展函数链式调用
这篇关于【kotlin】let with run apply also的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!