2. Mobile device developer
Johnny Sung
https://fb.com/j796160836
https://blog.jks.coffee/
https://www.slideshare.net/j796160836
https://github.com/j796160836
27. var isCreature = true
if (isCreature) {
print("是⽣生物")
} else {
print("不是⽣生物")
}
(⾃自動判斷為布林林值,省略略型態寫法)
28. 常⽤用運算元
== 等於
> ⼤大於
>= ⼤大於等於
< ⼩小於
<= ⼩小於等於
&& AND
|| OR
! NOT
真值表
A B A && B A || B
false false false false
false true false true
true false false true
true true true true
31. val viewType: Int = 1
when (viewType) {
1, 2 -> {
// Do something with value 1 or 2
}
3 -> {
// Do something with value 3
}
else -> {
// Do something with default
}
}
32. switch (viewType) {
case 1:
case 2:
// Do something with value 1 or 2
break;
case 3:
// Do something with value 3
break;
default:
// Do something with default
}
val viewType: Int = 1
when (viewType) {
1, 2 -> {
// Do something with value 1 or 2
}
3 -> {
// Do something with value 3
}
else -> {
// Do something with default
}
}
Java Kotlin
😮
33. Java Kotlin
if (item instanceof ModelA) {
// ...
} else if (item instanceof ModelB) {
// ...
} else if (item instanceof ModelC) {
// ...
} else {
// ...
}
when (item) {
is ModelA -> {
// ...
}
is ModelB -> {
// ...
}
is ModelC -> {
// ...
}
else -> {
// ...
}
}
😎
型態判斷
型態判斷
43. Java
Kotlin
val strings = arrayOf("a", "b", "c")
for ((index, value) in strings.withIndex()) {
println("The element at $index is $value")
}
String[] strings = new String[]{"a", "b", "c"};
for (int index = 0; index < strings.length; index++) {
String value = strings[index];
System.out.println("The element at " + index + " is " + value);
}
44. Kotlin
型態省略略
val strings = arrayOf("a", "b")
val strings = ArrayList<String>()
strings.add("a")
strings.add("b")
strings.add("c")
型態省略略
val num = 1
val str = "hello"
型態省略略
型態省略略
val str:String = "hello"
val num:Int = 1
45. Java
Kotlin
for (int i = 0; i < strings.length; i++) {
String string = strings[i];
// ...
}
for (String string : strings) {
// ...
}
for (string in strings) {
// ...
}
String[] strings = new String[]{"a", "b"};
陣列列
陣列列
val strings: Array<String> = arrayOf("a", "b")
for (i in 0 until strings.size) {
// ...
}
62. class MyClass {
private val str = "hello"
public fun printHello() {
println(str)
}
}
類別名
類別變數
類別⽅方法
63. public class MyClass {
private String str = "hello";
public void printHello() {
System.out.println(str);
}
}
class MyClass {
private val str = "hello"
public fun printHello() {
println(str)
}
}
Java Kotlin
64. class 類別 object 物件
https://auto.epochtimes.com/car-b5-36337-%E6%96%B0%E8%BB%8A%E4%BB%8B%E7%B4%B9%EF%BC%9A2019%E6%AC%BE%E5%A5%A7%E8%BF%AAAudi%20A6.html
https://itw01.com/V25GNEJ.html
65. class MyDataModel {
var name: String? = ""
var num: Int = 0
}
val model = MyDataModel()
model.name = "John"
model.num = 123
建立 MyDataModel 物件
67. Java Kotlin
class Student {
private String name;
public Student(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
}
class Student {
var name: String
constructor(name: String) {
this.name = name
}
fun printName(){
println(name)
}
}
建構⼦子
68. 建構⼦子的變形
class Student {
var name: String
constructor(name: String) {
this.name = name
}
fun printName(){
println(name)
}
}
class Student(var name: String) {
fun printName(){
println(name)
}
}
80. 變數轉型
• 物件的轉型
• 要是繼承樹裡的才能轉型
open class Father {
// ...
}
class Brother: Father() {
// ...
}
var a: Father = Brother()
var b = a as? Brother
如果轉型失敗就回傳 null
變數型態為:Brother?
102. class HairClipperForDog {
fun haircut(dog: Dog) {
println("(理理髮器聲...)")
dog.sound()
println("頭⽑毛理理好了了!")
}
}
class HairClipperForCat {
fun haircut(cat: Cat) {
println("(理理髮器聲...)")
cat.sound()
println("頭⽑毛理理好了了!")
}
}
val myCat = Cat()
val catClipper = HairClipperForCat()
catClipper.haircut(myCat)
val myDog = Dog()
val dogClipper = HairClipperForDog()
dogClipper.haircut(myDog)
103. class HairClipper<T : Animal> {
fun haircut(animal: T) {
println("(理理髮器聲...)")
animal.sound()
println("頭⽑毛理理好了了!")
}
}
https://tw.buy.yahoo.com/gdsale/
%E6%97%A5%E8%B1%A1%E9%BB%91%E9%91%BD%E9%9B%BB%E5%8B%95%E7%90%86%E9%AB%AE%E5%99%A8%E5%85%85%E6%8F%92%E6%9C%89%E7%B7%9A%E7%84%A1%E7%B7%9AZOH-2600C-5289698.html
⾃自⼰己取的型態 T
只限於 Animal 繼承樹上的
104. val myDog = Dog()
val hairClipperForDog = HairClipper<Dog>()
hairClipperForDog.haircut(myDog)
val myCat = Cat()
val hairClipperForCat = HairClipper<Cat>()
hairClipperForCat.haircut(myCat)