继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

From Java to Dart

慕的地10843
关注TA
已关注
手记 1081
粉丝 200
获赞 962

前言

帮助你快速入门 Dart

Print to Console

Java

System.out.print("Hello, World");
System.out.println("Hello, World");

Dart

print('Hello, World!');

Constants and Variables

Java

String name="LiLy";
final String name="LiLy";
int lineCount;  默认值是 0

Dart

var name = 'LiLy'   OR  String name="LiLy";
final name = 'LiLy';  OR  final String nickname = 'Bobby';
int lineCount;  默认值是 null

Verify if value is null

Java

if (text != null) {
   int length=text.length();
}

Dart

if (text != null) {
   int length=text.length();
}

Concatenation of strings

Java

String firstName = "A";
String lastName = "B";
String name = "My name is: " + firstName + " " + lastName;

Dart

String firstName = "A";
String lastName = "B";
String name = "My name is: ${firstName} ${lastName}" 
String name = "My name is: " + firstName + " " + lastName;  

New line in string

Java

String text = "First Line\n" +              "Second Line\n" +              "Third Line";

Dart

String text = "First Line\nSecond Line\nThird Line";

Ternary Operations

Java

String text = x > 5 ? "x > 5" : "x <= 5";
String message = null;
System.out.print(message != null ? message : "");

Dart

String text = x > 5 ? "x > 5" : "x <= 5";
String message = null; 
print(message != null ? message : "");

Bitwise Operators

Java

final 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;

Dart

final 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

Java

if (object instanceof Car) {
}

Dart

if (object is Car) { 
 }

Multiple conditions

Java

if (score >= 0 && score <= 300) {
 }

Dart

同上

Multiple Conditions (Switch case)

Java

int 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

Java

 for (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()) { }

Dart

for (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

Java

final 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");

Dart

final  List listOfNumber = [1, 2, 3, 4];
final Map keyValue = Map();
keyValue[1]="Amit";
keyValue[2]="Ali";
keyValue[3]="Mindorks";

Splitting arrays

Java

String[] splits = "param=car".split("=");
String param = splits[0];
String value = splits[1];

Dart

var splits = "param=car".split('=');
String param = splits[0]; 
String value = splits[1];

Defining methods

Java

void doSomething() {
 // logic here
 }
int  getInt(){
        return 2
}

Dart

同上 OR
int  getInt() => 2

Constructors

Java

public class Utils {
    private Utils() {
            // This utility class is not publicly instantiable
    }
    public static int getScore(int value) {
             return 2 * value;
     }
 }

Dart

class 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

Java

Person person;

Dart

var person;

Enum

Java

public enum Direction { 
      A,B,C
}

Dart

enum Direction { A,B,C }

Sorting List

Java

List 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;
        }
});

Dart

List profiles = loadProfiles(context); 
profiles.sort((a , b)=>a.age.compareTo(b.age));



作者:i校长
链接:https://www.jianshu.com/p/c33b54ce27ad


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP