开篇看定论
let
let扩展函数的实际上是一个效果域函数,当你需求去定义一个变量在一个特定的效果域范围内,let函数的是一个不错的选择;let函数另一个效果便是能够避免写一些判断null的操作。
- let函数的一般结构
object.let{
it.todo()//在函数体内运用it替代object目标去拜访其公有的特点和办法
...
}
//另一种用途 判断object为null的操作
object?.let{//表明object不为null的条件下,才会去履行let函数体
it.todo()
}
- let函数的kotlin和Java转化
//kotlin
fun main(args: Array<String>) {
val result = "testLet".let {
println(it.length)
1000
}
println(result)
}
//java
public 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);
}
}
- let函数运用前后的比照
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)
}
- let函数适用的场景
场景一: 最常用的场景便是运用let函数处理需求针对一个可null的目标统一做判空处理。 场景二: 然后便是需求去明确一个变量所在特定的效果域范围内能够运用
with
- with函数运用的一般结构
with(object){
//todo
}
- with函数的kotlin和Java转化
//kotlin
fun 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")
}
------------------------------------------------------------------------------------------------------------------------------
//java
public 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);
}
- with函数运用前后的比照
override fun onBindViewHolder(holder: ViewHolder, position: Int){
val item = getItem(position)?: return
with(item){
holder.tvNewsTitle.text = StringUtils.trimToEmpty(titleEn)
holder.tvNewsSummary.text = StringUtils.trimToEmpty(summary)
holder.tvExtraInf.text = "难度:$gradeInfo | 单词数:$length | 读后感: $numReviews"
}
}
------------------------------------------------------------------------------------------------------------------------------
@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);
}
- with函数的适用的场景 适用于调用同一个类的多个办法时,能够省去类名重复,直接调用类的办法即可,常常用于Android中RecyclerView中onBinderViewHolder中,数据model的特点映射到UI上
run
- run函数运用的一般结构
object.run{
//todo
}
- run函数的kotlin和Java转化
//java
public 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);
}
------------------------------------------------------------------------------------------------------------------------------
//kotlin
fun 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")
}
- run函数运用前后比照
override fun onBindViewHolder(holder: ViewHolder, position: Int){
val item = getItem(position)?: return
with(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"
...
}
}
- run函数运用场景
适用于let,with函数任何场景。由于run函数是let,with两个函数结合体,准确来说它弥补了let函数在函数体内有必要运用it参数替代目标,在run函数中能够像with函数相同能够省掉,直接拜访实例的公有特点和办法,另一方面它弥补了with函数传入目标判空问题,在run函数中能够像let函数相同做判空处理
apply
- apply函数运用的一般结构
object.apply{
//todo
}
- apply函数的kotlin和Java转化
//java
public final class ApplyFunctionKt {
public 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);
String var3 = "result: " + user;
System.out.println(var3);
}
}
//kotlin
fun main(args: Array<String>) {
val user = User("Kotlin", 1, "1111111")
val result = user.apply {
println("my name is $name, I am $age years old, my phone number is $phoneNum")
1000
}
println("result: $result")
}
- apply函数运用前后的比照
//运用前
mSheetDialogView = View.inflate(activity, R.layout.biz_exam_plan_layout_sheet_inner, null)
mSheetDialogView.course_comment_tv_label.paint.isFakeBoldText = true
mSheetDialogView.course_comment_tv_score.paint.isFakeBoldText = true
mSheetDialogView.course_comment_tv_cancel.paint.isFakeBoldText = true
mSheetDialogView.course_comment_tv_confirm.paint.isFakeBoldText = true
mSheetDialogView.course_comment_seek_bar.max = 10
mSheetDialogView.course_comment_seek_bar.progress = 0
//运用后
mSheetDialogView = View.inflate(activity, R.layout.biz_exam_plan_layout_sheet_inner, null).apply{
course_comment_tv_label.paint.isFakeBoldText = true
course_comment_tv_score.paint.isFakeBoldText = true
course_comment_tv_cancel.paint.isFakeBoldText = true
course_comment_tv_confirm.paint.isFakeBoldText = true
course_comment_seek_bar.max = 10
course_comment_seek_bar.progress = 0
}
//多级判空
if (mSectionMetaData == null || mSectionMetaData.questionnaire == null || mSectionMetaData.section == null) {
return;
}
if (mSectionMetaData.questionnaire.userProject != null) {
renderAnalysis();
return;
}
if (mSectionMetaData.section != null && !mSectionMetaData.section.sectionArticles.isEmpty()) {
fetchQuestionData();
return;
}
mSectionMetaData?.apply{
//mSectionMetaData不为空的时分操作mSectionMetaData
}?.questionnaire?.apply{
//questionnaire不为空的时分操作questionnaire
}?.section?.apply{
//section不为空的时分操作section
}?.sectionArticle?.apply{
//sectionArticle不为空的时分操作sectionArticle
}
also
- also函数运用的一般结构
object.also{
//todo
}
仿制
- also函数编译后的class文件
//java
public final class AlsoFunctionKt {
public static final void main(@NotNull String[] args) {
Intrinsics.checkParameterIsNotNull(args, "args");
String var2 = "testLet";
int var4 = var2.length();
System.out.println(var4);
System.out.println(var2);
}
}
//kotlin
fun main(args: Array<String>) {
val result = "testLet".also {
println(it.length)
1000
}
println(result)
}
- also函数的适用场景
适用于let函数的任何场景,also函数和let很像,仅仅唯一的不同点便是let函数最终的回来值是最终一行的回来值而also函数的回来值是回来当前的这个目标。一般可用于多个扩展函数链式调用
最终
假如你看到了这儿,觉得文章写得不错就给个赞呗?
更多Android进阶指南 能够详细Vx关注公众号:Android老皮 解锁 《Android十大板块文档》
1.Android车载应用开发体系学习指南(附项目实战)
2.Android Framework学习指南,助力成为体系级开发高手
3.2023最新Android中高级面试题汇总+解析,告别零offer
4.企业级Android音视频开发学习路线+项目实战(附源码)
5.Android Jetpack从入门到通晓,构建高质量UI界面
6.Flutter技术解析与实战,跨平台首要之选
7.Kotlin从入门到实战,全方面提高架构根底
8.高级Android插件化与组件化(含实战教程和源码)
9.Android 性能优化实战+360全方面性能调优
10.Android零根底入门到通晓,高手进阶之路
敲代码不易,关注一下吧。ღ( ・ᴗ・` )