场景一: 最常用的场景就是使用let函数处理需要针对一个可null的对象统一做判空处理。
场景二: 然后就是需要去明确一个变量所处特定的作用域范围内可以使用
with
with(object){
//todo
}
//kotlin
fun main(args: Array) {
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);
}
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 = “难度:
g
r
a
d
e
I
n
f
o
∣
单
词
数
:
gradeInfo | 单词数:
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
object.run{
//todo
}
//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) {
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”)
}
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 = “难度:
g
r
a
d
e
I
n
f
o
∣
单
词
数
:
gradeInfo | 单词数:
gradeInfo∣单词数:length | 读后感: KaTeX parse error: Expected 'EOF', got '}' at position 20: …eviews" ... }̲ } // 使用后 overr…gradeInfo | 单词数:$length | 读后感: $numReviews”
…
}
}
适用于let,with函数任何场景。因为run函数是let,with两个函数结合体,准确来说它弥补了let函数在函数体内必须使用it参数替代对象,在run函数中可以像with函数一样可以省略,直接访问实例的公有属性和方法,另一方面它弥补了with函数传入对象判空问题,在run函数中可以像let函数一样做判空处理
apply
object.apply{
//todo
}
//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) {
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”)
}
//使用前
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
object.also{
//todo
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
}
//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);
}