Dart 语法基础教程
1. 变量
Dart 中的变量使用 var
或明确的数据类型来声明,并使用 =
赋值。
var name = "Alice"; // 使用 var 声明
String message = "Hello, world!"; // 明确数据类型
- 变量名: 必须以字母、下划线
_
或美元符号$
开头,后续可以包含数字、字母和下划线。 - 数据类型: Dart 是强类型语言,每个变量都有明确的数据类型。常用的数据类型包括:
- 数字:
int
(整数) 和double
(浮点数) - 字符串:
String
- 布尔值:
bool
- 列表:
List
- 映射:
Map
- 数字:
2. 数据类型
2.1 数字
- 整数: 使用
int
表示,例如1
,-10
,0
。 - 浮点数: 使用
double
表示,例如3.14
,-2.718
,1.0
。
int age = 25;
double pi = 3.14159;
2.2 字符串
- 使用
String
表示,例如"Hello"
,"Dart"
。 - 可以使用单引号
'
或双引号"
来包含字符串。
String greeting = "Hello, world!";
String name = 'Alice';
2.3 布尔值
- 使用
bool
表示,只有true
和false
两个值。
bool isCompleted = true;
bool isRunning = false;
2.4 列表
- 使用
List
表示,是一个有序的数据集合,可以存储多个相同类型的值。 - 可以使用方括号
[]
来创建列表。
List<String> names = ["Alice", "Bob", "Charlie"];
List<int> numbers = [1, 2, 3, 4, 5];
2.5 映射
- 使用
Map
表示,是一个无序的键值对集合,键和值可以是不同的类型。 - 可以使用花括号
{}
来创建映射。
Map<String, int> ages = {"Alice": 25, "Bob": 30};
Map<String, String> languages = {"English": "English", "Spanish": "Español"};
3. 运算符
Dart 支持常见的算术、比较、逻辑、位运算符。
3.1 算术运算符
- 加法
+
- 减法
-
- 乘法
*
- 除法
/
- 模运算
%
int sum = 10 + 5; // sum = 15
int difference = 10 - 5; // difference = 5
int product = 10 * 5; // product = 50
double quotient = 10 / 5; // quotient = 2.0
int remainder = 10 % 3; // remainder = 1
3.2 比较运算符
- 等于
==
- 不等于
!=
- 大于
>
- 小于
<
- 大于等于
>=
- 小于等于
<=
bool isEqual = 10 == 10; // isEqual = true
bool isNotEqual = 10 != 5; // isNotEqual = true
bool isGreater = 10 > 5; // isGreater = true
bool isLess = 10 < 5; // isLess = false
bool isGreaterOrEqual = 10 >= 10; // isGreaterOrEqual = true
bool isLessOrEqual = 10 <= 10; // isLessOrEqual = true
3.3 逻辑运算符
- 逻辑与
&&
- 逻辑或
||
- 逻辑非
!
bool isTrue = true && true; // isTrue = true
bool isFalse = true && false; // isFalse = false
bool isTrue2 = true || false; // isTrue2 = true
bool isFalse2 = false || false; // isFalse2 = false
bool isNotTrue = !true; // isNotTrue = false
3.4 位运算符
- 按位与
&
- 按位或
|
- 按位异或
^
- 左移
<<
- 右移
>>
- 按位非
~
4. 控制流程
4.1 if 语句
- 用于根据条件执行不同的代码块。
if (condition) {
// 如果 condition 为真,执行此代码块
} else if (anotherCondition) {
// 如果 condition 为假,且 anotherCondition 为真,执行此代码块
} else {
// 如果所有条件都为假,执行此代码块
}
4.2 switch 语句
- 用于根据表达式的值执行不同的代码块。
switch (expression) {
case value1:
// 如果 expression 等于 value1,执行此代码块
break;
case value2:
// 如果 expression 等于 value2,执行此代码块
break;
default:
// 如果 expression 不匹配任何 case,执行此代码块
}
4.3 循环
- for 循环: 用于重复执行代码块固定次数。
for (int i = 0; i < 5; i++) {
// 执行此代码块 5 次
}
- while 循环: 用于重复执行代码块直到条件为假。
while (condition) {
// 只要 condition 为真,就执行此代码块
}
- do-while 循环: 用于至少执行一次代码块,然后根据条件决定是否继续执行。
do {
// 至少执行一次此代码块
} while (condition);
5. 函数
- 函数是可重复使用的代码块,可以接受参数并返回结果。
void sayHello(String name) {
print("Hello, $name!");
}
int sum(int a, int b) {
return a + b;
}
- 函数声明:
returnType
: 函数返回值的数据类型。functionName
: 函数的名称。parameters
: 函数的参数列表,每个参数都包含数据类型和名称。
- 函数调用:
functionName(arguments)
: 使用函数名称和参数列表来调用函数。
6. 类
- 类是对象的蓝图,用于定义对象的属性和方法。
class Person {
String name;
int age;
void sayHello() {
print("Hello, my name is $name.");
}
}
- 类声明:
class className
: 定义类的名称。properties
: 类的属性,使用data type name
声明。methods
: 类的方法,使用returnType methodName(parameters)
声明。
7. 实例
void main() {
// 变量
var name = "Alice";
String message = "Hello, world!";
// 数字
int age = 25;
double pi = 3.14159;
// 字符串
String greeting = "Hello, world!";
String name2 = 'Alice';
// 布尔值
bool isCompleted = true;
bool isRunning = false;
// 列表
List<String> names = ["Alice", "Bob", "Charlie"];
List<int> numbers = [1, 2, 3, 4, 5];
// 映射
Map<String, int> ages = {"Alice": 25, "Bob": 30};
Map<String, String> languages = {"English": "English", "Spanish": "Español"};
// 运算符
int sum = 10 + 5; // sum = 15
int difference = 10 - 5; // difference = 5
int product = 10 * 5; // product = 50
double quotient = 10 / 5; // quotient = 2.0
int remainder = 10 % 3; // remainder = 1
// 控制流程
if (age >= 18) {
print("You are an adult.");
} else {
print("You are not an adult yet.");
}
switch (age) {
case 18:
print("You are an adult.");
break;
case 25:
print("You are in your mid-twenties.");
break;
default:
print("Your age is not 18 or 25.");
}
for (int i = 0; i < 5; i++) {
print("Iteration: $i");
}
int count = 0;
while (count < 5) {
print("Count: $count");
count++;
}
// 函数
sayHello("Alice");
int result = sum(10, 5); // result = 15
print(result);
// 类
Person person = Person();
person.name = "Alice";
person.age = 25;
person.sayHello();
}
8. 总结
本教程涵盖了 Dart 语法基础,包括变量、数据类型、运算符、控制流程和函数。通过学习这些基础知识,您可以开始编写简单的 Dart 程序。
注意: 这是一个入门教程,还有很多高级的 Dart 特性尚未涵盖,例如泛型、异步编程、异常处理等等。您可以参考 Dart 官方文档 https://dart.dev/ 了解更多信息。