使用kotlin的時候對這幾個關鍵字的使用不是很明確,找了不少文章和書籍來加深一下印象,感謝mikyoujava
開篇看結論 bash
let擴展函數的其實是一個做用域函數,當你須要去定義一個變量在一個特定的做用域範圍內,let函數的是一個不錯的選擇;let函數另外一個做用就是能夠避免寫一些判斷null的操做。app
object.let{
it.todo()//在函數體內使用it替代object對象去訪問其公有的屬性和方法
...
}
//另外一種用途 判斷object爲null的操做
object?.let{//表示object不爲null的條件下,纔會去執行let函數體
it.todo()
}
複製代碼
//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);
}
}
複製代碼
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(object){
//todo
}
複製代碼
//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);
}
複製代碼
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);
}
複製代碼
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<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")
}
複製代碼
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"
...
}
}
複製代碼
適用於let,with函數任何場景。由於run函數是let,with兩個函數結合體,準確來講它彌補了let函數在函數體內必須使用it參數替代對象,在run函數中能夠像with函數同樣能夠省略,直接訪問實例的公有屬性和方法,另外一方面它彌補了with函數傳入對象判空問題,在run函數中能夠像let函數同樣作判空處理ide
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<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")
}
複製代碼
//使用前
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
}
複製代碼
object.also{
//todo
}
複製代碼
//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)
}
複製代碼
適用於let函數的任何場景,also函數和let很像,只是惟一的不一樣點就是let函數最後的返回值是最後一行的返回值而also函數的返回值是返回當前的這個對象。通常可用於多個擴展函數鏈式調用函數