dart 其实跟kotlin很像,而kotlin很早也被谷歌列为第一开发语言,那为何没有选择kotlin而使用了dart呢, 我也说不上来O(∩_∩)O~~。git
下面对比下dart vs kotlin:github
Kotlin数组
println("Hello, world!")
复制代码
Dart安全
print("Hello, world!");
复制代码
Kotlinbash
var myVar = 1
myVar = 2
val myConstant = 5
复制代码
Dartapp
var myVar = 1;
myVar = 2;
const myConstant = 5;
复制代码
Kotlin异步
val explicitVar: Int = 7
复制代码
Dartasync
int explicitVar = 7;
复制代码
Kotlin函数
val apples = 3
val oranges = 3
val message = "I have ${apples + oranges} fruits"
复制代码
Dart学习
var apples = 3;
var oranges = 3;
var message = "I have ${apples + oranges} fruits";
复制代码
Kotlin
val fruits = arrayOf("apple", "orange", "banana")
复制代码
Dart
var fruits = ["apple", "orange", "banana"];
复制代码
Kotlin
val basket = mutableMapOf(
"apple" to 3,
"orange" to 2
)
basket["apple"] = 5
复制代码
Dart
var basket = {
"apple": 3,
"orange": 2,
};
basket["apple"] = 5;
复制代码
Kotlin
fun greet(name: String): String {
return "Hello $name"
}
greet("Bob")
复制代码
Dart
String greet(String name) {
return "Hello $name";
}
greet("Bob");
复制代码
Kotlin
fun addIntro(title: String, subtitle: String = "No subtitle", bold: Boolean = true) {
// ...
}
addIntro("Title")
addIntro("Title", "My subtitle")
addIntro("Title", "My subtitle", false)
复制代码
Dart
void addIntro(String title, [String subtitle, bool bold]) {
// ...
}
addIntro("Title");
addIntro("Title", "My subtitle");
addIntro("Title", "My subtitle", false);
复制代码
Kotlin
class Foo {
private var bar = 0
}
复制代码
Dart
class Foo {
int _bar = 0; // underscore marks the field as private
int get bar => _bar;
set bar(int val) => _bar = val;
}
复制代码
Kotlin
val obj = "string"
if (obj is String) {
print(obj.length)
}
复制代码
Dart
var obj = "string";
if (obj is String) {
print(obj.length);
}
复制代码
Kotlin
var a: String = "abc"
a = null // compilation error
var b: String? = "abc"
b = null // ok
val length = b?.length // length will be null
复制代码
Dart
var a = "abc";
a = null;
var length = a.length; // NPE
length = a?.length; // length will be null
复制代码
Kotlin
// this will also generate equals, hashCode, toString methods
data class Box(val width: Int, val height: Int)
复制代码
Dart Dart没有这个特性,可是咱们能够经过代码生成的问题。可是咱们能够经过built_value包来生成。
abstract class Box implements Built<Box, BoxBuilder> {
int get width;
int get height;
}
复制代码
Kotlin
fun heavyComputation() {
async {
...
val result = computation.await()
...
}
}
复制代码
Dart
void heavyComputation() async {
...
var result = await computation();
...
}
复制代码
总结结语:
若是你熟悉kotlin,那么在学习dart的过程当中更容易更亲切。
动动手指头,点个赞呗。