dart 替代java_Dart与Java的语法区别
1. 主函數(shù)
(1)?沒有public static
(2) 命令參數(shù)List args
voidmain() {
}
2. 可以在class外定義變量,方法等
3. 沒有public, private, protected關(guān)鍵字
4. 創(chuàng)建對象,new可選
5. 獲取值${variableValue}, ${expression}
6. Class中屬性默認(rèn)public,若聲明私有,只需在屬性名前加_
classBicycle {intcadence;int _speed = 0;int get speed =>_speed;intgear;
Bicycle(this.cadence, this.gear);void applyBrake(intdecrement) {
_speed-=decrement;
}void speedUp(intincrement) {
_speed+=increment;
}
@override
String toString()=> 'Bicycle: $_speed mph';
}voidmain() {
var bike= Bicycle(2, 1);
print(bike);
}
7. getter/setter方法
//返回值類型/get/外部可訪問屬性/方法體
int get speed => _speed
//set/外部可訪問屬性(值)/方法體
set speed(int value) => _speed = value;
8. 未初始化的變量值均為null
9. 構(gòu)造函數(shù)簡化,避免重載
import 'dart:math';classRectangle {
Point origin;
num width;
num height;
Rectangle({this.origin = const Point(0, 0), this.width = 0, this.height = 0});
@override
String toString()=> 'Origin: (${origin.x}, ${origin.y}), width: $width, height: $height';
}voidmain() {
print(Rectangle(origin:const Point(3,4), width: 10, height: 20));
print(Rectangle(origin:const Point(10,20)));
print(Rectangle(width:20));
print(Rectangle());
}
10. 一個(gè)dart文件中可以定義多個(gè)class,也可以僅僅定義方法
//factory.dart
import 'shapes.dart';
Shape shapeFactory(String type){if(type == 'circle') return Circle(2);if(type == 'square') return Square(2);throw 'Can\'t create $type';
}
11. 字符串可用單引號或者雙引號
12. 工廠構(gòu)造函數(shù) => 在抽象類中定義工廠函數(shù),規(guī)定子類實(shí)例化方式
abstract classShape {//factory constructor
factory Shape(String type){if(type == 'circle') return Circle(10);if(type == 'square') return Square(10);throw 'can\'t create $type';
}
num get area;
}
13. Dart中沒有interface關(guān)鍵字,每個(gè)類都可以做接口
import 'shapes.dart';class CircleMock implementsCircle {
num area;
num radius;
}
//A person. The implicit interface contains greet().
classPerson {//In the interface, but visible only in this library.
final_name;//Not in the interface, since this is a constructor.
Person(this._name);//In the interface.
String greet(String who) => 'Hi $who, I\'m $_name';
}//An implementation of the Person interface.
class Impostor implementsPerson {
@override
String get _name=> this._name;
@override
String greet(String who)=> 'Hi $who, do you know who I am?';
}//polymorphism
String greetBob(Person person) => person.greet('Bob');
main() {
print(greetBob(Person('chris')));
print(greetBob(Impostor()));
}
14. 同一對象級聯(lián)操作
通過..符號可以在同一對象上級聯(lián)操作。
querySelector('#confirm') //Get an object.
..text = 'Confirm' //Use its members.
..classes.add('important')
..onClick.listen((e)=> window.alert('Confirmed!'));
15. 函數(shù)無需聲明可能拋出的異常類型
16. 捕獲異常
try{//···
} on Exception catch(e) {
print('Exception details:\n $e');
}catch(e, s) {
print('Exception details:\n $e');
print('Stack trace:\n $s');
}
17. 在運(yùn)行時(shí),Dart保留泛型類型信息
var names = List();
names.addAll(['Seth', 'Kathy', 'Lars']);
print(names is List); //true
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的dart 替代java_Dart与Java的语法区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: 并发编程(十六)——java7 深入并发
 - 下一篇: 翁同龢后人向上海博物馆捐赠两件重要家藏