dart 數(shù)據(jù)類型

一、變量和常量

1.變量 var

dart語言的變量定義與JavaScript定義變量一樣的,使用var 關(guān)鍵字。變量沒有賦值,默認值為null。

void main(){
 var name = 'amy';
  print(name)
}
1.常量 const final

使用const ,final定義常量時,必須要初始化,否則會報錯。定義完之后,不能再重新賦值。

void main(){
  const name = 'amy';
  print(name)
  final age = 18;
  print(age)
}

二、數(shù)值型

數(shù)值型有int(整型),double(浮點型),dart的數(shù)值型的方式和屬性與javascript差不多,不過,dart的數(shù)值型多了一個‘~/’,相除求整的運算符

1.運算符
+,-,*,/,%,~/
void main(){
  int a = 10;
  double b = 12.3;
  print(a+b);
  print(a-b);
  print(a*b);
  print(a/b);
  print(a~/b);    // 除求整
  print(a%b);
  print(0.0/0.0); //nan
}
2.方法和屬性
void main(){
  int a = 10;
  double b = 12.3;
 
  print(a.isEven);
  print(a.isFinite);
  print(a.isInfinite);
  print(a.isNaN);
  print(a.isOdd);

  print(a.abs());
  print(a.ceil());

}

三.字符串

1.字符串定義

字符串定義可以使用單引號,雙引號,三引號來定義

void main(){
  var str = 'hello dart';
  var str1 = """hello
                world""";
  var str2 = 'hello \n amy';
  var str3 = r'hello \n amy';

  print(str);
  print(str1);
  print(str2);
  print(str3);
}

注意:
1.三引號定義用于多行字符串的顯示,連換行和空格也會打印出來;
2.字符串使用普通方式定義,且中間有轉(zhuǎn)譯符,會識別轉(zhuǎn)義符;
3.字符串定義前面加‘r’會把字符串的內(nèi)容原本輸出

2.字符串的運算符
+,* ,==,[]
void main(){
  String str = 'hello dart';
  print(str+'hahha');
  print(str*5);   // 遍歷5次連接起來
  print(str[0]);
}
3.字符串插值表達式
+,* ,==,[]
void main(){
  var a =1;
  var b =2;
  print('a+b = ${a+b}');  // 字符串占位符
  print('a = $a');
}

如果有表達式,使用大括號括起來,如果只是一個變量,就直接寫$,

4.字符串的屬性和方法
void main(){
  String str = 'hello dart';

  print(str.trim());
  print(str.padLeft(20,'a'));// 在字符串的左邊補指定字符串(默認為空格)

  print(str.length);
  print(str.isEmpty);
}

屬性,方法有很多,可以到官網(wǎng)或源碼看看,這里就不一一列舉了。

5.布爾型

布爾型就兩種,false和true

void main(){
  bool isFalse = false;
  bool isTrue = true;
  print(isFalse);
  print(isTrue);
}

6.List型(Array)

1.List的定義
void main(){
  var list = [1,2,3,'22'];        
  var list1 = const [4,5,6];
  var list2 = new List();
}

使用const定義的list,不可修改

2.LIST的方法屬性

屬性:主要有[],length等
方法:add(),insert(),remove(),clear()等
方法太多了,不想寫了

7.Map型

8.dynamic型

與JavaScript不同的地方
1.算術(shù)運算符??

void main(){
  var aa;
  var bb = 'js';
  print(aa??bb);
}
如果aa沒有值,值就為bb,如果aa有值,結(jié)果就為aa

2.switch--case 的continue 跳轉(zhuǎn)標(biāo)簽

void main(){
  var aa = 'dart';
  switch(aa){
    D:
    case 'dart':
      print('dart');
      break;
    case 'js':
      print('js');
      continue D;
    case 'python':
      print('python');
      break;
  }
}
打印出來:
js
dart

8.函數(shù)

(1)可選參數(shù)為對象

void main(List argc){
print(printPeople('amy',age:18));
  print(printPeople('amy',age:18));
}
printPeople(String name,{int age,String genger}){
  return 'I am $name,$age,$genger';
}

函數(shù)的可選參數(shù)為對象時,如果函數(shù)調(diào)用使用可選參數(shù),必選填寫參數(shù)名

(2)可選參數(shù)為數(shù)組

printPeople1(String name,[int age,String genger]){
  return 'I am $name,$age,$genger';
}
void main(List argc){
 print(printPeople1('amy'));
  print(printPeople1('amy',16,'女'));
}

可選參數(shù)為數(shù)組時,調(diào)用函數(shù)使用可選函數(shù),直接寫入對應(yīng)下標(biāo)的值就可以。
(3)可選參數(shù)設(shè)置默認值

printPeople2(String name,{int age = 30,String genger = 'male'}){
  return 'I am $name,$age,$genger';
}
void main(List argc){
  print(printPeople2('amy'));
  print(printPeople2('amy',genger:'famale'));
}

重新賦值會把默認值覆蓋,沒有賦值就為默認數(shù)據(jù)。

9.類

void main(List argc){

  var person = new Person();
  person.name='amy';
  person.age=18;
  print(person.name);
  person.say();
}

class Person{
  String name;
  int age;
  final String father = 'bab'; // final 定義的變量沒有setter方法,只能getter
  void say(){
    print('hi,我可以說話');
  }
  // void say(int age){           // 方法不能重載
  //   print('hi,我可以說話');
  // }
}

(1)類的聲明使用class
(2)類的創(chuàng)建使用new ,new 可省
(3)類中定義的實例變量都有g(shù)etter和setter屬性,但是final聲明的變量只有g(shù)etter.,沒有setter.
(4) 方法不能重構(gòu)

10 .類的計算屬性

void main(){
  var rect = new Rectangle();
  rect.width=10;
  rect.height=20;
  print(rect.getRect());
  print(rect.area);
  rect.area = 200;
  print(rect.width);
  
}
class Rectangle{
  num width,height;
  num getRect(){
    return width*height;
  }
  num get area=> width*height;
      set area(value){
        width = value/2;
      }
}

計算屬性首先聲明類型 ,然后一個get/set 再定義計算屬性的名稱,ok,看上面代碼

11.構(gòu)造函數(shù)

1.默認構(gòu)造函數(shù)

定義類對象的時候,如果不寫構(gòu)造函數(shù),默認會有一個構(gòu)造函數(shù)

void main(){
  print('hello world');
  var people = new People();
  people.name='amy';
  people.age=18;
  print(people.name);
}

class People {
  String name;
  int age;
  People(){}
  final String gender = '男';
}

2.重寫構(gòu)造函數(shù)

void main(){
  print('hello world');
  var people = new People('amy',18);
  print(people.name);
}

class People {
  String name;
  int age;
  final String gender = '男';
  People(String name,int age){
    this.name = name;
    this.age = age;
  }
}

3.構(gòu)造函數(shù)的語法糖

void main(){
  print('hello world');
  var people = new People('amy',18);
  print(people.name);
}

class People {
  String name;
  int age;
  final String gender = '男';
  People(this.name,this.age);
}

4.具名構(gòu)造函數(shù)(重構(gòu))

void main(){
  print('hello world');
  var people = new People('amy',18);
  new People.withName('ye');
  var age = new People.withAge(18);
  print(people.name);
  print(age.age);
}

class People {
  String name;
  int age;
  final String gender = '男';
  People(this.name,this.age);
  People.withName(this.name);
  People.withAge(this.age);
}

12.常量構(gòu)造函數(shù)

void main(){
  var people = [new] [const] People('amy',18);
  print(people.age);
}

class People {
  final String name;
  final int age;
  const People(this.name,this.age);
}

常量構(gòu)造函數(shù):
(1)類的定義必須使用final;
(2)類的構(gòu)造函數(shù)使用const 聲明;
(3)new 類對象時const可以省略

13.工廠構(gòu)造函數(shù)

void main(){
  var people = new People('amy',18);
  print(people.name);
}

class People {
  String name;
  int age;
  factory People(String name,int age){
    if(name!=null){
      return People._withName(name);
    }else{
      return People._withAge(age);
    }
      
  }
  People._withName(this.name);
  People._withAge(this.age);
}

普通的構(gòu)造函數(shù)是不能有返回值的,但是使用factor來定義的工廠構(gòu)造函數(shù)是可以有返回值;

14.初始化列表

void main(){
  var people = new People('amy',18);
  Map map = {'name':'胖金妹','age':1,'gender':'女'};
  
  var pp = new People.withMap(map);
  print(pp.name);
  print(people.name);
}

class People {
  String name;
  int age;
  String gender = 'nv';
  People(this.name,this.age);
  People.withMap(Map map):name = map['name'],age=map['age']{
    this.gender = map['gender'];
  }
}

15.靜態(tài)成員

void main(){
  var people = new People();
  People.scrollDown();
  people.scrollUp();
 
}

class People {
  // 類中的常量必須使用static定義
  static  const int maxPage = 10;
  // 靜態(tài)變量,
  static int currentPage = 0;
  // 靜態(tài)方法,實例不能直接調(diào)用,使用類調(diào)用
  static void scrollDown(){
    currentPage ++;
    print('scrolldown');
  }
  void scrollUp(){
    currentPage--;
    print('scrollUp');
  }
}

(1)定義類中的靜態(tài)變量和靜態(tài)方法,使用static關(guān)鍵字
(2)靜態(tài)成員不能訪問非靜態(tài)成員,非靜態(tài)成員可以訪問靜態(tài)成員
(3)類中的常量需要使用static const 聲明

16 對象操作符

1、.?條件成員訪問

void main(){
  People people = new People();
  people?.work(); // 條件成員訪問
}

class People {
 String name;
 int age;
 void work(){
   print('work');
 }
}

當(dāng)不確定返回對象是否為空時,使用條件成員訪問來判斷

2、.as類型的轉(zhuǎn)換

void main(){
  var people;
  people ='';
  people = new People();
  (people as People).work(); // 類型的轉(zhuǎn)換
  
}

class People {
 String name;
 int age;
 void work(){
   print('work');
 }
}

3、?,?!判斷符

void main(){
  var people;
  people ='';
  people = new People();
  if(people is People){
    print('true');
    people.work();
  }
  if(people is! People){
    print('false');

  }
}

class People {
 String name;
 int age;
 void work(){
   print('work');
 }
}

使用?判斷左邊的實例是否是右邊的類型。

4、..級聯(lián)運算符

void main(){
 var people = new People();
  people..name='amy'..age=18;
  print(people.name);
}

class People {
 String name;
 int age;
 void work(){
   print('work');
 }
}

17call方法的使用

void main(){
 var people = new People();
  people..name='amy'..age=18;
  print(people.name);
  people(); //對象作為方法來使用
}

class People {
 String name;
 int age;

  void call(){
    print('name:$name,age:$age');
  }
}

call 方法定義,可以使實例對象作為一個方法來使用

void main(){
 var people = new People();
  people..name='amy'..age=18;
  print(people.name);
  people('tom',1); //對象作為方法來使用
}

class People {
 String name;
 int age;

 void call(String name,int age){
   print('name:$name,age:$age');
 }
}

如果call方法聲明了形參,調(diào)用實例對象時,也要傳入對應(yīng)的參數(shù)

18.類的繼承

// people.dart
void main(){
 var people = new People();
}

class People {
 String name;
 int age;
 String _address;
 bool get isAdult=> age>18;
 void run (){
   print('people run');
 } 
}



// student.dart
import 'people.dart';
void main() {
  var student = new Student();
  student.study();
  student.name='amy';
  student.age=12;
  // student._address = 'ddd';// 不能繼承父類的私有屬性
  print(student.isAdult);
  student.run();
}
class Student extends People{
  void study(){
    print('student study');
  }
  void run(){  // 重寫父類的方法
    print('student run');
  }
  @override  // 重寫父類的計算屬性
  // TODO: implement isAdult
  bool get isAdult => this.age>16;
}

dart的繼承與其他語言的差不多,
(1)使用extends關(guān)鍵字實現(xiàn)繼承
(2)子類可以繼承父類的可見屬性和方法
(3)子類可以重寫父類的方法,getter,setter方法。

19.繼承中的構(gòu)造方法

1.子類的構(gòu)造方法會調(diào)用父類的無名無參的構(gòu)造方法

void main(){
 var stu = new Student();
}

class People {
 String name;
 int age;
 People(){
   print('People');
 }
 void run (){
   print('people run');
 } 
}

class Student extends People{
  void study(){
    print('student study');
  }
}

//編譯結(jié)果:People

子類的構(gòu)造方法調(diào)用父類的無名有參的構(gòu)造方法

void main(){
 var stu = new Student('amy');
 print(stu.name);
}

class People {
 String name;
 int age;
 People(this.name);
}

class Student extends People{
  Student(String name) : super(name);
  void study(){
    print('student study');
  }
}

2.子類的構(gòu)造方法調(diào)用父類有參有名的構(gòu)造方法

void main(){
 var stu = new Student(16);
 print(stu.age);
}

class People {
 String name;
 int age;
 People(this.name);
 People.withAge(this.age);
 
}

class Student extends People{
  Student(int age) : super.withAge(age); // 初始化列表

  void study(){
    print('student study');
  }
}

如果父類的構(gòu)造方法中不僅有無名構(gòu)造方法,也有具有構(gòu)造方法,在子類實現(xiàn)繼承的時候,可以通過選擇

20.抽象類

void main(){
  var stu = new Student();
  stu.run();
}

abstract class People {
 void run(){}
}

class Student extends People{
    void run(){
      print('重寫抽象類的方法');
    }
}

(1)抽象類通過abstract 關(guān)鍵字來創(chuàng)建
(2)抽象類不能通過new表達式實現(xiàn)實例化;
(3)通過子類繼承抽象類來實現(xiàn)抽象類的方法和屬性;
(4)抽象類的方法前面不需要abstract修飾,抽象類的方法可以為空;

21.接口

void main(){
  var stu = new Student();
  stu.run();
}

abstract class People {
 void run(){}
}

class Student implements People{
    void run(){
      print('重寫抽象類的方法');
    }
}

(1)接口會重新類的所有屬性和方法,可以是任意的類

22.Mixins 實現(xiàn)多繼承



void main(){
  var d = new D();
  d.a();
  d.b();
  d.c();
  d.d();
}

class A{
  void a(){
    print('A --a');
  }
}

class B{
  void b(){
    print('B--b');
  }
}

class C{
  void c(){
    print('C--c');
  }
}
//  mixins 實現(xiàn)類似多繼承的效果
class D extends A with B,C{
  void d(){
    print('d--d');
  }
}



void main(){
  var d = new D();
  d.a();    // B--a
}

class A{
  void a(){
    print('A --a');
  }
}

class B{
  void a(){
    print('B --a');
  }
  void b(){
    print('B--b');
  }
}

class C{
  void a(){
    print('C --a');
  }
  void b(){
    print('C--b');
  }
  void c(){
    print('C--c');
  }
}
//  mixins 實現(xiàn)類似多繼承的效果
class D extends A with C,B{
   
  void d(){
    print('D--d');
  }
}


如果多個類中有相同的方法,調(diào)用的是最后一個類方法

注意:
(1)Mixins類似于多繼承,是在多繼承中重用一個類代碼的方法‘
(2)作為Mixins的類不能有顯性聲明構(gòu)造方法
(3)作為Mixins的類只能繼承自O(shè)bject
(4)使用關(guān)鍵字with連接一個或多個Mixins

綜合的案例:



void main(){
  var car = new Car();
  var bus = new Bus();
  car.work();
  bus.work();
}

abstract class Engine{
  void work();
}
class OilEngine implements Engine{
  void work(){
    print('燒油引擎');
  }
}
class ElectricEngine implements Engine{
  void work(){
    print('燒電引擎');
  }
}

class Tyre{
  String name;
  void run(){
    print('跑起來啦');
  }
}

class Car = Tyre with ElectricEngine;
class Bus = Tyre with OilEngine;
// 不同的寫法
[class Car extends Tyre with OilEngine{}]

23.dart的枚舉

void main(){
  var currentSeason = Season.autumn;
  switch(currentSeason){
    case Season.spring:
      print('1-3月');
      break;
    case Season.summer:
      print('4-6月');
      break;
    case Season.autumn:
      print('7-9月');
      break;
    case Season.winter:
      print('10-12月');
      break;
  }
}

enum Season{
  spring,
  summer,
  autumn,
  winter
}

(1)index從0開始,依次累加
(2)不能指定原始值
(3)不能添加方法

24.泛型

dart中類型是可選的,可使用泛型限定類型

void main(){
  var list = new List<int>();
  list.add(1);
  print(list);  
}

類的泛型

void main(){
  var utilsInt = new Units<int>();
  utilsInt.put(1);
  var utilsStr = new Units<String>();
  utilsStr.put('amy');
}
class Units<T>{
  T element;
  void put(T element){
    this.element = element;
  }
}

方法的泛型

void main(){
  var units = new Units();
  units.put<int>(2);
  
}
class Units{
  
  void put<T>(T element){
    print(element);
  }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容