Dart语法复习

135 阅读6分钟

变量

1.可以使用var来定义变量,变量的类型可以通过变量值推断出来

var name = "hi"; //string类型
var age = 18;//int类型
var high = 1.70;//double类型

如上变量定义后其类型已经确定,不可再将其他类型的值赋给变量.

var name = "hi";//stirng类型
name = 3;//此处编译器会报错,name被定义赋值之后已经是一个String类型,不可再赋值int类型值

2.也可以使用特定类型来定义变量

String name = "jack";//string类型
int age = 18;//int类型

3.如果变量不限于单个类型,则可以使用dynamic或object来定义变量

dynamic value = 18;
print('value = $value');
value = "bruce";
print('value = $value');
value = 3.5;
print('value = $value');

Object val = 18;
print("val = $val");
val = "bruce";
print("val = $val");
val = 3.5;
print("val = $val");

//输出结果
value = 18
value = bruce
value = 3.5
val = 18
val = bruce
val = 3.5

变量的默认值

能够放在变量中的所有内容都是对象,所以如果一个变量没有初始化值,那它的默认值就为null。

int value1;
print("value1 = $value1");
bool value2;
print("value2 = $value2");
var value3;
print("value3 = $value3");
dynamic value4;
print("value4 = $value4");

//输出结果为
value1 = null
value2 = null
value3 = null
value4 = null

final和const

如果不打算更改变量,可以使用final或者const.一个final变量只能被设置一次,而const是编译时常量,定义时必须赋值.

//Person类
class Person{
	staic const desc = "This is a Person class";//必须定义时赋值,否则编译时报错
	final name;
	Person(this.name);//对象初始化时赋值一次
}

// 定义一个Person对象
Person p = Person("Bruce"); //创建对象时设置一次name
print("p.name = ${p.name}"); //可正常输出 p.name = Bruce
p.name = "haha"; //编译器报错

数据类型

dart语言支持一下数据类型

  • numbers

    包含int和double两种类型,没有像Java中的float类型,int和double都是num的子类型。

  • strings

    Dart的字符串是一系列UTF-16代码单元。创建方法如下:

    String str1 = "hello";
    print("str1 = $str1");
    String str2 = """Hi,Bruce
    This is Xiaoming.
    """; //使用带有单引号或双引号的三重引号可以创建多行字符串
    print("str2 = $str2");
    
    //输出结果为
    str1 = hello
    str2 = Hi,Bruce
      This is Xiaoming.
    
  • booleans

    Dart有一个名为bool的类型,只有两个对象是bool类型:true和false,他们都是编译时常量。

  • lists

    和其他编程语言常见的集合一样,Dart中使用的集合是数组或有序的对象组。Dart中数组是List对象。

    List arr = ["Bruce", "Nick", "John"];
    print("arr = $arr");
    
  • maps

Map map = {
	"name":"bruce",
	"age":18,
	"high":1.75
};

print("map = $map");
print("map['name'] = ${map['name']}");

var map1 ={
1:"hi",
2:"hello",
3:"yep"
};
print("map1 = $map1");
print("map1[1] = ${map1[1]}");


//输出结果
map = {name: bruce, age: 18, high: 1.75}
map['name'] = bruce
map1 = {1: hi, 2: hello, 3: yep}
map1[1] = hi

  • runes

    符文是字符串的UTF-32代码点。在字符串中表示32位Unicode值需要特殊语法,常用方法是 \uXXXX,其中XXXX是4位十六进制值,比如小心心(♥)是\u2665。要指定多于或少于4个十六进制数字,请将值放在大括号中。 比如,微笑(😆)是\u{1f600}

    String smile = '\u{1f600}';
    print("微笑:$smile");
    
    Runes input = new Runes(
      '\u2665  \u{1f605}  \u{1f60e}  \u{1f47b}  \u{1f596}  \u{1f44d}');
    print(String.fromCharCodes(input));
    
    //输出结果
    微笑:😀
    ♥  😅  😎  👻  🖖  👍
    

函数

Dart是一种真正的面向对象语言,因此即使是函数也是对象并且具有类型Function。这意味着函数可以分配给变量或作为参数传递给其他函数。

定义方法

String getName() {
  return "Bruce";
}

如果函数体中只包含一个表达式,则可以使用简写语法

String getName() => "Bruce";

可选参数

Dart函数可以设置可选参数,可以使用命名参数也可以使用位置参数。

命名参数,定义格式如{param1,param2,...}

//函数定义
void showDesc({var name,var age}){
	if(name != null){
		print("name = $name");
	}
	if(age != null){
		print("age = $age");
	}
}
//函数调用
showDesc(name:"bruce");

输出结果
name = bruce

位置参数,使用[]来标记可选参数

//函数定义
void showDesc(var name,[var age]){
	print("name = $name");
	if(age !=null){
		print("age = $age");
	}
}
//调用
showDesc("jon");

//输出结果
name=jon

默认值

函数的可选参数也可以使用=设置默认值

//函数定义
void showDesc(var name,[var age = 18]){
	print("name = $name");
	if(age != null){
		print("age = $age");
	}
}
showDesc("jon");

//输出结果
name=jon
age = 18

main函数

和其他编程语言一样,Dart中每个应用程序都必须有一个顶级main()函数,该函数作为应用程序的入口点。

函数作为参数

Dart中的函数可以作为另一个函数的参数。

// 函数定义
void println(String name) {
  print("name = $name");
}

void showDesc(var name, Function log) {
  log(name);
}
showDesc('jon',println);

//输出结果
name = jon

匿名函数

void showDesc(var name, Function log){
	log(name);
}

ShowDesc('jon',(name){
	print("name = $name");
});

//输出结果
name = jon

嵌套函数

Dart支持嵌套函数,也就是函数中可以定义函数.

//函数定义
void showDesc(var name){
  print("that is anested funciton");
  void println(String name){
	print("name = $name");
	}
	log(name);
}
showDesc("bruce");

//输出结果
That is a nested function
name = bruce

运算符

Description Operator
一元后缀 expr++expr--()[].?.
一元前缀 -expr!expr~expr++expr--expr
乘除操作 */%~/
加减操作 + -
移位 << >>
按位操作 & ^ |
比较关系和类型判断 == != >=><=<asisis!
逻辑关系 && ||
是否为null ??
条件语句操作 expr1 ? expr2 : expr3
级联操作 ..
分配赋值操作 =*=/=~/=%=+=-=<<=>>=&=^=
  • ?. 的使用
//定义类
class Person{
	var name;
	person(this.name);
}

Person p;
var name = p?.name;//先判断p是否为null,如果是,则name为null;否则返回p.name的值
print("name = $name");

//输出结果
name = null;
  • ~/ 的使用

var num = 10; var result = num ~/ 3;//得出一个小于等于(num/3)的最大整数 print("result = $result");

// 输出结果 result = 3

  • as 的使用,as用来做类型转化
class Banana{
	var weight;
	Banana(this.weight);
}

class Apple{
	var weight;
	Apple(this.weght);
}

dynamic b = Banana(20);
(b as Banana).weight = 20;
print("b.wight = ${(b as Banana).weight}");
(b as Apple).weight = 30;//类型转换错误,运行报错
print("b.weight = ${(b as Apple).weight}");

//输出结果
b.weight = 20
Uncaught exception:
CastError: Instance of 'Banana': type 'Banana' is not a subtype of type 'Apple'
  • is 的使用,判断对象是否为某类
getFruit() => Banana(20);//获取一个水果对象

class Banana{
	var weight;
	Banana(this.weight);
}

class Apple{
	var color;
	Apple(this.color);
}

var b = getFruit();
if(b is Apple){//判断对象是否为Apple类
	print("The fruit is an apple");
}else if(b is Banana){
	print("The fruit is a banana");
}

// 输出结果
The fruit is a banana
  • ?? 的使用
String name;
String nickName = name ?? "Nick";//如果name不为null,则nickName值为name的值,否则值为Nick

// 输出结果
nickName = Nick
  • .. 的使用,级联操作允许对同一个对象进行一系列操作.
class Banana{
	var weight;
	var color;
	Banana(this.weight,this.color);
	
	void showWeight(){
		print("weight = $weight");
	}
	
	void showColor(){
		print("color = $color");
	}
}

Banana(20,'yellow')
			..showWeight()
			..showColor();
			
// 输出结果
weight = 20
color = yellow

控制流语句

  • if and else
  • for 循环
  • while 和 do-while循环
  • break和continue
  • switch-case语句

以上控制流语句和其他编程语言用法一样,switch-case有一个特殊的用法如下,可以使用continue语句和标签来执行指定case语句.

var fruit = 'apple';
switch (fruit){
	case "banana":
		print("this is a banana");
		continue anotherFruit;
	anotherFruit:
	case "apple":
		print("this is an apple");
		break;
}

// 输出结果
this is an apple

异常

Dart的异常捕获也是使用try-catch语法

// 定义一个抛出异常的函数
void handleOperator() => throw Exception("this operator exception!");
try{
	handleOperator();
}on Exception catch(e){
	print(e);
}finally{
	print("finally");
}

// 输出结果
Exception: this operator exception!
finally

Dart是一种面向对象的语言,具有类和基于mixin的继承。Dart的所有类也都继承自Object。

构造函数

Dart的构造函数同普通函数一样,可以定义无参和有参,命名参数和位置参数,可选参数和给可选参数设置默认值等。Dart的构造函数有以下几个特点:

  1. 可以定义命名构造函数
  2. 可以在函数体运行之前初始化实例变量
  3. 子类不从父类继承构造函数,定义没有构造函数的子类只有无参无名称的构造函数
  4. 子类定义构造函数时默认继承父类无参构造函数,也可继承制定有参数的构造函数;
//命名构造函数和函数体运行前初始化实例变量
class Tree{
	var desc;
	// 命名构造函数
	Tree.init(){
		desc = "this is a seed";
	}
	// 函数体运行之前初始化实例变量
	Tree(var des) : desc = des;
}

Tree t = Tree.init();
print("${t.desc}");

Tree t1 = Tree("this is a tree");
print("${t1.desc}");

// 输出结果
this is a seed
this is a tree

构造函数继承

class Fruit{
	Fruit(){
		print("this is Fruit constructor with no param");
	}
	Fruit.desc(var desc){
		print("$desc in Fruit");
	}
}

class Apple extends Fruit{
	Apple():super(){
		print("this is Apple constructor with no param");
	}
	// 默认继承无参构造函数
	Apple.desc(var desc){
		print('$desc in Apple');
	}
}

Apple();
Apple.desc("say hello");

//输出结果
this is Fruit constructor with no param
this is Apple constructor with no param
this is Fruit constructor with no param
say hello in Apple

mixin继承

mixin是一种在多个类层次结构中重用类代码的方法.

class LogUtil{
	void log(){
		print("this is a log");
	}
}

class Fruit{
	Fruit(){
		print("this is Fruit constructor with no param");
	}
}

class Apple extends Fruit with LogUtil{
	Apple():super(){
		print("this is Apple constructor with no param");
	}
}

Apple a = Apple();
a.log(); //可执行从LogUtil继承过来的方法

// 输出结果
this is Fruit constructor with no param
this is Apple constructor with no param
this is a log

泛型

Dart同swift一样,也支持泛型

class Apple{
	var desc;
	Apple(this.desc);
	void log(){
		print("${this.desc}");
	}
}

class Banana{
	var desc;
	Banana(this.desc);
	void log(){
		print("${this.desc}");
	}
}

class FruitFactory<T> {
		T produceFruit(T t){
			return t;
		}
}

FruitFactory<Banana> f = FruitFactory<Banana>();
Banana b = f.produceFruit(Banana("a banana"));
b.log();

FruitFactory<Apple> f1 = FruitFactory<Apple>();
Apple a = f1.produceFruit(Apple("an apple"));
a.log();

// 输出结果
a banana
an apple

本文来源:一文了解Dart语法