欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)中Dart語言7個很酷的特點(diǎn)

 更新時間:2022年05月17日 09:02:00   作者:會煮咖啡的貓  
這篇文章主要為大家介紹了Android開發(fā)中Dart語言7個很酷的特點(diǎn)分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

參考

https://dart.dev/guides/language/language-tour

正文

今天的文章簡短地揭示了 Dart 語言所提供的很酷的特性。更多時候,這些選項對于簡單的應(yīng)用程序是不必要的,但是當(dāng)你想要通過簡單、清晰和簡潔來改進(jìn)你的代碼時,這些選項是一個救命稻草。

考慮到這一點(diǎn),我們走吧。

Cascade 級聯(lián)

Cascades (.., ?..) 允許你對同一個對象進(jìn)行一系列操作。這通常節(jié)省了創(chuàng)建臨時變量的步驟,并允許您編寫更多流暢的代碼。

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 抽象類

使用 abstract 修飾符定義一個 _abstract 抽象類(無法實例化的類)。抽象類對于定義接口非常有用,通常帶有一些實現(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 工廠建造者

在實現(xiàn)不總是創(chuàng)建類的新實例的構(gòu)造函數(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ù)為一個類實現(xiàn)多個構(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 是在多個類層次結(jié)構(gòu)中重用類代碼的一種方法。

要實現(xiàn) implement mixin,創(chuàng)建一個聲明沒有構(gòu)造函數(shù)的類。除非您希望 mixin 可以作為常規(guī)類使用,否則請使用 mixin 關(guān)鍵字而不是類。

若要使用 mixin,請使用后跟一個或多個 mixin 名稱的 with 關(guān)鍵字。

若要限制可以使用 mixin 的類型,請使用 on 關(guān)鍵字指定所需的超類。

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

類型別名ー是指代類型的一種簡明方式。通常用于創(chuàng)建在項目中經(jīng)常使用的自定義類型。

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)有庫和代碼中添加功能的方法。

//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ù)

通過將位置參數(shù)包裝在方括號中,可以使位置參數(shù)成為可選參數(shù)??蛇x的位置參數(shù)在函數(shù)的參數(shù)列表中總是最后一個。除非您提供另一個默認(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)您想要啟動一個 Future 時,建議的方法是使用 unawaited

否則你不加 async 就不會執(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開發(fā)Dart語言7個很酷的特點(diǎn)的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)Dart特點(diǎn)的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論