From fd53bbb2400beba13bc70b0cecdb9db4fd46b38b Mon Sep 17 00:00:00 2001 From: caohuijun2018 Date: Thu, 11 Jul 2019 22:43:16 +0800 Subject: [PATCH] part1 --- src/i_introduction/_0_Hello_World/n00Start.kt | 2 +- .../n10ObjectExpressions.kt | 16 +++++++++++++--- .../_11_SAM_Conversions/n11SAMConversions.kt | 2 +- .../n12ExtensionsOnCollections.kt | 4 ++-- .../n01JavaToKotlinConverter.kt | 13 ++++++++++++- .../_2_Named_Arguments/n02NamedArguments.kt | 4 ++-- .../_3_Default_Arguments/n03DefaultArguments.kt | 15 +++++++++------ src/i_introduction/_4_Lambdas/n04Lambdas.kt | 2 +- .../_5_String_Templates/n05StringTemplates.kt | 2 +- .../_6_Data_Classes/n06DataClasses.kt | 5 ++--- .../_7_Nullable_Types/n07NullableTypes.kt | 8 +++++++- .../_8_Smart_Casts/n08SmartCasts.kt | 4 ++-- .../n09ExtensionFunctions.kt | 4 ++-- 13 files changed, 55 insertions(+), 26 deletions(-) diff --git a/src/i_introduction/_0_Hello_World/n00Start.kt b/src/i_introduction/_0_Hello_World/n00Start.kt index 352ca5bfc..5158a296d 100644 --- a/src/i_introduction/_0_Hello_World/n00Start.kt +++ b/src/i_introduction/_0_Hello_World/n00Start.kt @@ -25,5 +25,5 @@ fun todoTask0(): Nothing = TODO( ) fun task0(): String { - return todoTask0() + return "OK" } \ No newline at end of file diff --git a/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt b/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt index ef12d8127..5293c49d2 100644 --- a/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt +++ b/src/i_introduction/_10_Object_Expressions/n10ObjectExpressions.kt @@ -5,7 +5,7 @@ import util.doc10 import java.util.* fun todoTask10(): Nothing = TODO( - """ + """ Task 10. Read about object expressions that play the same role in Kotlin as anonymous classes in Java. @@ -13,11 +13,21 @@ fun todoTask10(): Nothing = TODO( In Kotlin you use Kotlin library extensions instead of java.util.Collections, but this example is still a good demonstration of mixing Kotlin and Java code. """, - documentation = doc10() + documentation = doc10() ) fun task10(): List { val arrayList = arrayListOf(1, 5, 2) - Collections.sort(arrayList, todoTask10()) + Collections.sort(arrayList, { x, y -> y-x}) + fun xxx(x: Int, y: Int): Int { + return y - x + } + Collections.sort(arrayList, fun(x: Int, y: Int): Int { + return y - x + }) return arrayList +} + +fun xxx(x: Int, y: Int): Int { + return y - x } \ No newline at end of file diff --git a/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt b/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt index 5f021921c..68a3dd5bc 100644 --- a/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt +++ b/src/i_introduction/_11_SAM_Conversions/n11SAMConversions.kt @@ -15,6 +15,6 @@ fun todoTask11(): Nothing = TODO( fun task11(): List { val arrayList = arrayListOf(1, 5, 2) - Collections.sort(arrayList, { x, y -> todoTask11() }) + Collections.sort(arrayList, { x, y -> y - x }) return arrayList } diff --git a/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt b/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt index ae453a63b..8008fa92f 100644 --- a/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt +++ b/src/i_introduction/_12_Extensions_On_Collections/n12ExtensionsOnCollections.kt @@ -17,7 +17,7 @@ fun todoTask12(): Nothing = TODO( ) fun task12(): List { - todoTask12() - return arrayListOf(1, 5, 2) + + return arrayListOf(1, 5, 2).sortedDescending() } diff --git a/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt b/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt index f7a40f5e8..bcab8face 100644 --- a/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt +++ b/src/i_introduction/_1_Java_To_Kotlin_Converter/n01JavaToKotlinConverter.kt @@ -14,5 +14,16 @@ fun todoTask1(collection: Collection): Nothing = TODO( fun task1(collection: Collection): String { - todoTask1(collection) + val sb = StringBuilder() + sb.append("{") + val iterator = collection.iterator() + while (iterator.hasNext()) { + val element = iterator.next() + sb.append(element) + if (iterator.hasNext()) { + sb.append(", ") + } + } + sb.append("}") + return sb.toString() } \ No newline at end of file diff --git a/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt b/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt index 11fc442bb..8cdef7759 100644 --- a/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt +++ b/src/i_introduction/_2_Named_Arguments/n02NamedArguments.kt @@ -24,6 +24,6 @@ fun todoTask2(): Nothing = TODO( references = { collection: Collection -> task1(collection); collection.joinToString() }) fun task2(collection: Collection): String { - todoTask2() - return collection.joinToString() + + return collection.joinToString(prefix="{",postfix="}") } \ No newline at end of file diff --git a/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt b/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt index b7adbd670..649db72a1 100644 --- a/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt +++ b/src/i_introduction/_3_Default_Arguments/n03DefaultArguments.kt @@ -14,12 +14,15 @@ fun todoTask3(): Nothing = TODO( documentation = doc2(), references = { name: String -> JavaCode3().foo(name); foo(name) }) -fun foo(name: String): String = todoTask3() +fun foo(name: String, number: Int=42, toUpperCase : Boolean=false): String { + if(toUpperCase == true ) { return name.toUpperCase() + number } else { return name + number} +} + fun task3(): String { - todoTask3() -// return (foo("a") + -// foo("b", number = 1) + -// foo("c", toUpperCase = true) + -// foo(name = "d", number = 2, toUpperCase = true)) + + return (foo("a") + + foo("b", number = 1) + + foo("c", toUpperCase = true) + + foo(name = "d", number = 2, toUpperCase = true)) } \ No newline at end of file diff --git a/src/i_introduction/_4_Lambdas/n04Lambdas.kt b/src/i_introduction/_4_Lambdas/n04Lambdas.kt index 3c174fc77..5b9acb2cb 100644 --- a/src/i_introduction/_4_Lambdas/n04Lambdas.kt +++ b/src/i_introduction/_4_Lambdas/n04Lambdas.kt @@ -22,4 +22,4 @@ fun todoTask4(collection: Collection): Nothing = TODO( documentation = doc4(), references = { JavaCode4().task4(collection) }) -fun task4(collection: Collection): Boolean = todoTask4(collection) \ No newline at end of file +fun task4(collection: Collection): Boolean =collection.any{ x -> x%2==0} \ No newline at end of file diff --git a/src/i_introduction/_5_String_Templates/n05StringTemplates.kt b/src/i_introduction/_5_String_Templates/n05StringTemplates.kt index 7b7f9b625..b2e94bc34 100644 --- a/src/i_introduction/_5_String_Templates/n05StringTemplates.kt +++ b/src/i_introduction/_5_String_Templates/n05StringTemplates.kt @@ -35,4 +35,4 @@ fun todoTask5(): Nothing = TODO( documentation = doc5(), references = { getPattern(); month }) -fun task5(): String = todoTask5() +fun task5(): String = """\d{2} ${month} \d{4}""" diff --git a/src/i_introduction/_6_Data_Classes/n06DataClasses.kt b/src/i_introduction/_6_Data_Classes/n06DataClasses.kt index 1ce6b8796..0669a68ac 100644 --- a/src/i_introduction/_6_Data_Classes/n06DataClasses.kt +++ b/src/i_introduction/_6_Data_Classes/n06DataClasses.kt @@ -15,10 +15,9 @@ fun todoTask6(): Nothing = TODO( references = { JavaCode6.Person("Alice", 29) } ) -class Person + data class Person(var name: String , var age: Int ) fun task6(): List { - todoTask6() - return listOf(/*Person("Alice", 29), Person("Bob", 31)*/) + return listOf(Person("Alice", 29), Person("Bob", 31)) } diff --git a/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt b/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt index 1d0109436..2f188ce68 100644 --- a/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt +++ b/src/i_introduction/_7_Nullable_Types/n07NullableTypes.kt @@ -25,7 +25,13 @@ fun todoTask7(client: Client?, message: String?, mailer: Mailer): Nothing = TODO fun sendMessageToClient( client: Client?, message: String?, mailer: Mailer ) { - todoTask7(client, message, mailer) + if (client == null || message == null) return + + val personalInfo = client.personalInfo ?: return + + val email = personalInfo.email ?: return + + mailer.sendMessage(email, message) } class Client(val personalInfo: PersonalInfo?) diff --git a/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt b/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt index 6f3a6675e..e89b131a1 100644 --- a/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt +++ b/src/i_introduction/_8_Smart_Casts/n08SmartCasts.kt @@ -12,8 +12,8 @@ class Sum(val left: Expr, val right: Expr) : Expr() fun eval(e: Expr): Int = when (e) { - is Num -> todoTask8(e) - is Sum -> todoTask8(e) + is Num -> (e as Num).value + is Sum -> eval(e.left) + eval(e.right) } fun todoTask8(expr: Expr): Nothing = TODO( diff --git a/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt b/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt index 75506f232..8d7af5889 100644 --- a/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt +++ b/src/i_introduction/_9_Extension_Functions/n09ExtensionFunctions.kt @@ -29,7 +29,7 @@ fun todoTask9(): Nothing = TODO( data class RationalNumber(val numerator: Int, val denominator: Int) -fun Int.r(): RationalNumber = todoTask9() -fun Pair.r(): RationalNumber = todoTask9() +fun Int.r(): RationalNumber = RationalNumber( this,1) +fun Pair.r(): RationalNumber =RationalNumber( first,second)