移动端六大语言速记:第2部分 - 控制结构
本文继续对比Java、Kotlin、Flutter(Dart)、Python、ArkTS和Swift这六种移动端开发语言的控制结构,帮助开发者快速掌握各语言的语法差异。
2. 控制结构
2.1 条件语句
各语言条件语句的语法对比:
语言 | if语句 | 三元运算符 | switch/case | 空值处理 |
---|---|---|---|---|
Java | if (condition) {...} else if (condition) {...} else {...} |
condition ? value1 : value2 |
switch(var){case val: ... break;} |
if(obj != null) |
Kotlin | if (condition) {...} else if (condition) {...} else {...} |
if (condition) value1 else value2 |
when(var){value -> ...} |
?. , ?: , !! |
Dart | if (condition) {...} else if (condition) {...} else {...} |
condition ? value1 : value2 |
switch(var){case val: ... break;} |
?. , ?? , !. |
Python | if condition: ... elif condition: ... else: ... |
value1 if condition else value2 |
无原生支持,用字典或if-elif | if obj is not None |
ArkTS | if (condition) {...} else if (condition) {...} else {...} |
condition ? value1 : value2 |
switch(var){case val: ...} |
?. , ?? |
Swift | if condition {...} else if condition {...} else {...} |
condition ? value1 : value2 |
switch var {case val: ...} |
? , ?? , ! |
示例对比
Java:
// if-else语句
int score = 85;
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "D";
}
// 三元运算符
boolean isPassed = score >= 60 ? true : false;
// switch语句
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
// ... 其他情况
default:
dayName = "Invalid day";
}
// 空值处理
String text = null;
if (text != null) {
System.out.println(text.length());
}
Kotlin:
// if-else语句
val score = 85
val grade = if (score >= 90) {
"A"
} else if (score >= 80) {
"B"
} else if (score >= 70) {
"C"
} else {
"D"
}
// Kotlin使用if-else作为表达式
val isPassed = if (score >= 60) true else false
// when语句 (类似switch)
val day = 3
val dayName = when (day) {
1 -> "Monday"
2 -> "Tuesday"
3 -> "Wednesday"
// ... 其他情况
else -> "Invalid day"
}
// 空值处理
val text: String? = null
// 安全调用操作符
val length = text?.length // 如果text为null,则length为null
// Elvis操作符
val nonNullLength = text?.length ?: 0 // 如果text为null,则nonNullLength为0
// 非空断言操作符
try {
val forcedLength = text!!.length // 如果text为null,抛出NullPointerException
} catch (e: Exception) {
println("Caught exception: $e")
}
Dart:
// if-else语句
int score = 85;
String grade;
if (score >= 90) {
grade = "A";
} else if (score >= 80) {
grade = "B";
} else if (score >= 70) {
grade = "C";
} else {
grade = "D";
}
// 三元运算符
bool isPassed = score >= 60 ? true : false;
// switch语句
int day = 3;
String dayName;
switch (day) {
case 1:
dayName = "Monday";
break;
case 2:
dayName = "Tuesday";
break;
case 3:
dayName = "Wednesday";
break;
// ... 其他情况
default:
dayName = "Invalid day";
}
// 空值处理
String? text = null;
// 安全调用操作符
int? length = text?.length; // 如果text为null,则length为null
// 空值合并操作符
int nonNullLength = text?.length ?? 0; // 如果text为null,则nonNullLength为0
Python:
# if-else语句
score = 85
if score >= 90:
grade = "A"
elif score >= 80:
grade = "B"
elif score >= 70:
grade = "C"