前言
帮助你快速入门 Dart
Print to Console
JavaSystem.out.print("Hello, World");
System.out.println("Hello, World");
Dartprint('Hello, World!');
Constants and Variables
JavaString name="LiLy";
final String name="LiLy";
int lineCount; 默认值是 0
Dartvar name = 'LiLy' OR String name="LiLy";
final name = 'LiLy'; OR final String nickname = 'Bobby';
int lineCount; 默认值是 null
Verify if value is null
Javaif (text != null) {
int length=text.length();
}
Dartif (text != null) {
int length=text.length();
}
Concatenation of strings
JavaString firstName = "A";
String lastName = "B";
String name = "My name is: " + firstName + " " + lastName;
DartString firstName = "A";
String lastName = "B";
String name = "My name is: ${firstName} ${lastName}"
String name = "My name is: " + firstName + " " + lastName;
New line in string
JavaString text = "First Line\n" +
"Second Line\n" +
"Third Line";
DartString text = "First Line\nSecond Line\nThird Line";
Ternary Operations
JavaString text = x > 5 ? "x > 5" : "x <= 5";
String message = null;
System.out.print(message != null ? message : "");
DartString text = x > 5 ? "x > 5" : "x <= 5";
String message = null;
print(message != null ? message : "");
Bitwise Operators
Javafinal int andResult = a & b;
final int orResult = a | b;
final int xorResult = a ^ b;
final int rightShift = a >> 2;
final int leftShift = a << 2;
final int unsignedRightShift = a >>> 2;
Dartfinal int andResult = a & b;
final int orResult = a | b;
final int xorResult = a ^ b;
final int rightShift = a >> 2;
final int leftShift = a << 2;
最后一个不知道,官方文档也没查到,谁要是知道可以留言,谢谢🙏
Check the type and casting
Javaif (object instanceof Car) {
}
Dartif (object is Car) {
}
Multiple conditions
Javaif (score >= 0 && score <= 300) {
}
Dart同上
Multiple Conditions (Switch case)
Javaint score = // some score;
String grade;
switch (score) {
case 10:
case 9:
grade = "Excellent";
break;
case 8:
case 7:
case 6:
grade = "Good";
break;
case 5:
case 4:
grade = "OK";
break;
case 3:
case 2:
case 1:
grade = "Fail";
break;
default:
grade = "Fail";
}
Dart同上
For-loops
Javafor (int i = 1; i <= 10 ; i++) { }
for (int i = 1; i < 10 ; i++) { }
for (int i = 10; i >= 0 ; i--) { }
for (int i = 1; i <= 10 ; i+=2) { }
for (int i = 10; i >= 0 ; i-=2) { }
for (String item : collection) { }
for (Map.Entry entry: map.entrySet()) { }
Dartfor (int i = 1; i <= 10 ; i++) { }
for (int i = 1; i < 10 ; i++) { }
for (int i = 10; i >= 0 ; i--) { }
for (int i = 1; i <= 10 ; i+=2) { }
for (int i = 10; i >= 0 ; i-=2) { }
for (String item in collection) { } OR collection.forEach((item)=>print(item));
map.forEach((k, v) {
print(' $k and $v');
});
Collections
Javafinal List listOfNumber = Arrays.asList(1, 2, 3, 4);
final Map keyValue = new HashMap();
map.put(1, "Amit");
map.put(2, "Ali");
map.put(3, "Mindorks");
// Java 9
final List listOfNumber = List.of(1, 2, 3, 4);
final Map keyValue = Map.of(1, "Amit",
2, "Ali",
3, "Mindorks");
Dartfinal List listOfNumber = [1, 2, 3, 4];
final Map keyValue = Map();
keyValue[1]="Amit";
keyValue[2]="Ali";
keyValue[3]="Mindorks";
Splitting arrays
JavaString[] splits = "param=car".split("=");
String param = splits[0];
String value = splits[1];
Dartvar splits = "param=car".split('=');
String param = splits[0];
String value = splits[1];
Defining methods
Javavoid doSomething() {
// logic here
}
int getInt(){
return 2
}
Dart同上 OR
int getInt() => 2
Constructors
Javapublic class Utils {
private Utils() {
// This utility class is not publicly instantiable
}
public static int getScore(int value) {
return 2 * value;
}
}
Dartclass Utils {
Utils() {
// This utility class is not publicly instantiable
}
static int getScore(int value) {
return 2 * value;
}
}
Dart中有没有C++或Java中表示访问权限的private、public关键字
凡以“_”(下划线)开头的符号(变量、类、函数等等)都是包内可见的,否则是包内外都可见的
Defining uninitialized objects
JavaPerson person;
Dartvar person;
Enum
Javapublic enum Direction {
A,B,C
}
Dartenum Direction { A,B,C
}
Sorting List
JavaList profiles = loadProfiles(context);
Collections.sort(profiles, new Comparator() {
@Override
public int compare(Profile profile1, Profile profile2) {
if (profile1.getAge() > profile2.getAge()) return 1;
if (profile1.getAge() < profile2.getAge()) return -1;
return 0;
}
});
DartList profiles = loadProfiles(context);
profiles.sort((a , b)=>a.age.compareTo(b.age));
总结
整体比较下来,还是特别容易学的,基本一致。有任何问题,欢迎留言一起探讨。