This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout | |
| xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical" | |
| tools:context=".grader.LegendaryGraderActivity"> | |
| <TextView |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| interface Grader { | |
| fun grade(score: Int): String | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class TypicalGrader : Grader { | |
| override fun grade(score: Int): String = | |
| when (score) { | |
| in 80..100 -> "A" | |
| in 70..79 -> "B" | |
| in 60..69 -> "C" | |
| in 50..59 -> "D" | |
| in 0..49 -> "F" | |
| else -> "???" | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SuGrader(val satisfy: Int = 60) : Grader { | |
| init { | |
| if (satisfy < 0 || satisfy > 100) | |
| throw IllegalArgumentException("A satisfy $satisfy is out of score range of 0-100") | |
| } | |
| override fun grade(score: Int) = when (score) { | |
| in satisfy..100 -> "S" | |
| in 0 until satisfy -> "U" | |
| else -> "???" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import kotlinx.android.synthetic.main.activity_legendary_grader.* | |
| class LegendaryGraderActivity : AppCompatActivity() { | |
| var grader: Grader = TypicalGrader() | |
| val satisfy = 50 | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_legendary_grader) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| apply plugin: 'com.android.application' | |
| apply plugin: 'kotlin-android' | |
| apply plugin: 'kotlin-android-extensions' | |
| android { | |
| compileSdkVersion 24 | |
| buildToolsVersion "24.0.0" | |
| defaultConfig { | |
| applicationId "com.travispea.kotlinforandroid101" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public void attack(Target target) { | |
| attack(target, new Fist()); | |
| } | |
| public void attack(Target target, Weapon weapon) { | |
| attack(target, weapon, 0.1f); | |
| } | |
| public void attack(Target target, Weapon weapon, float critical) { | |
| //... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun attack(target: Target, weapon: Weapon = Fist(), critical: Float = 0.1F) { | |
| //... | |
| } | |
| // <paramName>: <paramType> [= <defaultValue>] | |
| val target = getCurrentTarget() | |
| attack(target) | |
| attack(target, Sword()) | |
| attack(target, Bow(), 0.5F) | |
| attack(target = target, critical = 0.9f) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static String formatFancy(Date date) { | |
| String myFancyFormat = ... | |
| SimpleDateFormat f = new SimpleDateFormat(myFancyFormat); | |
| return f.format(date); | |
| } | |
| String fancyDate = MyDateUtil.formatFancy(someDate); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fun Date.formatFancy(): String { | |
| val myFancyFormat = ... | |
| val f = SimpleDateFormat(myFancyFormat) | |
| return f.format(this) | |
| } | |
| // fun <receiverType>.<functionName>(): <returnType> | |
| someDate.formatFancy() //call within Kotlin | |
| MyExtnsionsKt.formatFancy(someDate); //call from Java |
OlderNewer