Android開(kāi)發(fā)中Dart語(yǔ)言7個(gè)很酷的特點(diǎn)
參考
https://dart.dev/guides/language/language-tour
正文
今天的文章簡(jiǎn)短地揭示了 Dart 語(yǔ)言所提供的很酷的特性。更多時(shí)候,這些選項(xiàng)對(duì)于簡(jiǎn)單的應(yīng)用程序是不必要的,但是當(dāng)你想要通過(guò)簡(jiǎn)單、清晰和簡(jiǎn)潔來(lái)改進(jìn)你的代碼時(shí),這些選項(xiàng)是一個(gè)救命稻草。
考慮到這一點(diǎn),我們走吧。
Cascade 級(jí)聯(lián)
Cascades (.., ?..) 允許你對(duì)同一個(gè)對(duì)象進(jìn)行一系列操作。這通常節(jié)省了創(chuàng)建臨時(shí)變量的步驟,并允許您編寫(xiě)更多流暢的代碼。
var paint = Paint(); paint.color = Colors.black; paint.strokeCap = StrokeCap.round; paint.strokeWidth = 5.0; //above block of code when optimized var paint = Paint() ..color = Colors.black ..strokeCap = StrokeCap.round ..strokeWidth = 5.0;
Abstract 抽象類(lèi)
使用 abstract 修飾符定義一個(gè) _abstract 抽象類(lèi)(無(wú)法實(shí)例化的類(lèi))。抽象類(lèi)對(duì)于定義接口非常有用,通常帶有一些實(shí)現(xiàn)。
// This class is declared abstract and thus
// can't be instantiated.
abstract class AbstractContainer {
// Define constructors, fields, methods...
void updateChildren(); // Abstract method.
}
Factory constructors 工廠建造者
在實(shí)現(xiàn)不總是創(chuàng)建類(lèi)的新實(shí)例的構(gòu)造函數(shù)時(shí)使用 factory 關(guān)鍵字。
class Logger {
String name;
Logger(this.name);
factory Logger.fromJson(Map<String, Object> json) {
return Logger(json['name'].toString());
}
}
Named 命名構(gòu)造函數(shù)
使用命名構(gòu)造函數(shù)為一個(gè)類(lèi)實(shí)現(xiàn)多個(gè)構(gòu)造函數(shù)或者提供額外的清晰度:
class Points {
final double x;
final double y;
//unnamed constructor
Points(this.x, this.y);
// Named constructor
Points.origin(double x,double y)
: x = x,
y = y;
// Named constructor
Points.destination(double x,double y)
: x = x,
y = y;
}
Mixins 混合物
Mixin 是在多個(gè)類(lèi)層次結(jié)構(gòu)中重用類(lèi)代碼的一種方法。
要實(shí)現(xiàn) implement mixin,創(chuàng)建一個(gè)聲明沒(méi)有構(gòu)造函數(shù)的類(lèi)。除非您希望 mixin 可以作為常規(guī)類(lèi)使用,否則請(qǐng)使用 mixin 關(guān)鍵字而不是類(lèi)。
若要使用 mixin,請(qǐng)使用后跟一個(gè)或多個(gè) mixin 名稱(chēng)的 with 關(guān)鍵字。
若要限制可以使用 mixin 的類(lèi)型,請(qǐng)使用 on 關(guān)鍵字指定所需的超類(lèi)。
class Musician {}
//creating a mixin
mixin Feedback {
void boo() {
print('boooing');
}
void clap() {
print('clapping');
}
}
//only classes that extend or implement the Musician class
//can use the mixin Song
mixin Song on Musician {
void play() {
print('-------playing------');
}
void stop() {
print('....stopping.....');
}
}
//To use a mixin, use the with keyword followed by one or more mixin names
class PerformSong extends Musician with Feedback, Song {
//Because PerformSong extends Musician,
//PerformSong can mix in Song
void awesomeSong() {
play();
clap();
}
void badSong() {
play();
boo();
}
}
void main() {
PerformSong().awesomeSong();
PerformSong().stop();
PerformSong().badSong();
}
Typedefs
類(lèi)型別名ー是指代類(lèi)型的一種簡(jiǎn)明方式。通常用于創(chuàng)建在項(xiàng)目中經(jīng)常使用的自定義類(lèi)型。
typedef IntList = List<int>;
List<int> i1=[1,2,3]; // normal way.
IntList i2 = [1, 2, 3]; // Same thing but shorter and clearer.
//type alias can have type parameters
typedef ListMapper<X> = Map<X, List<X>>;
Map<String, List<String>> m1 = {}; // normal way.
ListMapper<String> m2 = {}; // Same thing but shorter and clearer.
Extension 擴(kuò)展方法
在 Dart 2.7 中引入的擴(kuò)展方法是一種向現(xiàn)有庫(kù)和代碼中添加功能的方法。
//extension to convert a string to a number
extension NumberParsing on String {
int customParseInt() {
return int.parse(this);
}
double customParseDouble() {
return double.parse(this);
}
}
void main() {
//various ways to use the extension
var d = '21'.customParseDouble();
print(d);
var i = NumberParsing('20').customParseInt();
print(i);
}
可選的位置參數(shù)
通過(guò)將位置參數(shù)包裝在方括號(hào)中,可以使位置參數(shù)成為可選參數(shù)??蛇x的位置參數(shù)在函數(shù)的參數(shù)列表中總是最后一個(gè)。除非您提供另一個(gè)默認(rèn)值,否則它們的默認(rèn)值為 null。
String joinWithCommas(int a, [int? b, int? c, int? d, int e = 100]) {
var total = '$a';
if (b != null) total = '$total,$b';
if (c != null) total = '$total,$c';
if (d != null) total = '$total,$d';
total = '$total,$e';
return total;
}
void main() {
var result = joinWithCommas(1, 2);
print(result);
}
unawaited_futures
當(dāng)您想要啟動(dòng)一個(gè) Future 時(shí),建議的方法是使用 unawaited
否則你不加 async 就不會(huì)執(zhí)行了
import 'dart:async';
Future doSomething() {
return Future.delayed(Duration(seconds: 5));
}
void main() async {
//the function is fired and awaited till completion
await doSomething();
// Explicitly-ignored
//The function is fired and forgotten
unawaited(doSomething());
}以上就是Android開(kāi)發(fā)Dart語(yǔ)言7個(gè)很酷的特點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于Android開(kāi)發(fā)Dart特點(diǎn)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開(kāi)發(fā)中Dart語(yǔ)言7個(gè)很酷的特點(diǎn)
這篇文章主要為大家介紹了Android開(kāi)發(fā)中Dart語(yǔ)言7個(gè)很酷的特點(diǎn)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Dart多態(tài)控制反轉(zhuǎn)編碼規(guī)范實(shí)例詳解
這篇文章主要為大家介紹了Dart多態(tài)控制反轉(zhuǎn)編碼規(guī)范實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Flutter 語(yǔ)法進(jìn)階抽象類(lèi)和接口本質(zhì)區(qū)別詳解
這篇文章主要為大家介紹了Flutter 語(yǔ)法進(jìn)階抽象類(lèi)和接口本質(zhì)區(qū)別詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
flutter中如何使用和擴(kuò)展ThemeData實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了flutter中如何使用和擴(kuò)展ThemeData實(shí)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
Flutter學(xué)習(xí)筆記(三)RowColum布局
這篇文章主要介紹了Flutter學(xué)習(xí)筆記(三)RowColum布局,通俗來(lái)說(shuō),就是橫向布局和縱向布局的用法,需要的朋友可以參考下2023-04-04
Dart語(yǔ)法之變量聲明與數(shù)據(jù)類(lèi)型實(shí)例詳解
這篇文章主要為大家介紹了Dart語(yǔ)法之變量聲明與數(shù)據(jù)類(lèi)型實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
Dart多個(gè)future隊(duì)列完成加入順序關(guān)系及原子性論證
這篇文章主要介紹了Dart多個(gè)future隊(duì)列完成加入順序關(guān)系及原子性論證,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11

