dart
1.hello,world
//Hello,world
void main() {
print('Hello, World!');
}
2.类型
2.1变量及常量
void main() {
const PI = 3.14; //常量不可改变
final a = new DateTime.now(); //常量,输出当前时间
var hello = ('helllo,world'); //var类型变量
int mynum = 1234; //int类型变量
double Mynum = 3.333; //double类型变量
var b = mynum + Mynum; //运算符
String str = '你好'; //String类型变量
bool flag = true; //bool类型变量
print(PI);
print(a);
print(hello);
print(mynum);
print(Mynum);
print(b);
print(str);
print(flag);
}
2.2类型判断
void main() {
var str = 1234;
if (str is String) {
print('是String类型');
} else {
print('是其他类型');
}
}
2.3类型转换(只写一个示例代码)
void main() {
String str1 = '123';
var mynum1 = int.parse(str1);
print(mynum1 is int);
var mynum2 = 12;
var str2 = mynum2.toString();
print(str2 is String);
}
3.字符串
3.1输出多行字符串
void main() {
String str1 = '''hello,world
hello,world
'''; //输出多行字符串
print(str1);
}
3.2字符串拼接
void main() {
String str1 = 'hello';
String str2 = 'world';
print("$str1 $str2"); //字符串拼接
print(str1 + str2);
print(str1 + " " + str2);
}
5.list
5.1定义list
void main() {
var l1 = ['张三', 20, true]; //定义List的第一种方法
print(l1);
print(l1.length);
print(l1[0]);
var l2 = <String>['张三', '李四']; //定义List的第二种方法 指定类型
print(l2);
var l3 = []; //定义List的第三种方法 增加数据
l3.add("张三");
l3.add(20);
print(l3);
var l4 = List.filled(2, ''); //定义List的第三种方法 固定集合和改变集合内容
print(l4);
l4[0] = '张三';
l4[1] = '李四';
print(l4);
}
5.2.list常用属性
void main() {
List l4 = <String>['香蕉', '苹果', '西瓜'];
print(l4);
print(l4.length);
print(l4.isEmpty);
print(l4.isNotEmpty);
print(l4.reversed);
var l5 = l4.reversed.toList();
print(l5);
}
5.3.list常用方法
void main() {
List l6 = ['香蕉', '苹果', '西瓜'];
print(l6);
l6.add('桃子');
l6.addAll(['草莓', '葡萄']);
print(l6);
print(l6.indexOf('香蕉'));
l6.remove('草莓');
l6.removeAt(2);
print(l6);
l6.fillRange(1, 2, 'aaa');
print(l6);
l6.insert(0, '栗子');
print(l6);
l6.insertAll(3, ['芒果', '橘子']);
print(l6);
var str1 = l6.join(',');
print(str1);
var str2 = "栗子,香蕉,芒果,橘子,桃子,葡萄";
var l7 = str2.split(',');
print(l7);
}
6.map
6.1.定义map
void main() {
var person1 = {'name': '张三', 'age': 20}; //定义Maps的第一种方法
print(person1);
print(person1['age']);
var person2 = new Map(); //定义Maps的第二种方法
person2['name'] = '李四';
person2['age'] = 22;
print(person2);
}
6.2map常用属性和方法
void main() {
var person = {"name": "Alice", "age": 20};
print(person);
print(person.keys);
print(person.keys.toList());
print(person.values.toList());
print(person.isEmpty);
print(person.isNotEmpty);
person.addAll({"height": 165, "work": "公务员"});
print(person);
print(person.containsValue("Alice"));
}
7.计算
7.1算数运算符
void main() {
int a = 15;
int b = 7;
print(a + b); //计算ab数值
print(a - b);
print(a * b);
print(a / b);
print(a % b);
print(a ~/ b);
print(a == b); //判断ab关系
print(a != b);
print(a > b);
print(a < b);
print(a >= b);
print(a <= b);
}
7.2逻辑运算符
void main() {
bool flag1 = true; //逻辑运算符
bool flag2 = false;
print(!flag1);
print(flag1 && flag2);
print(flag1 || flag2);
}
8.条件表达式
8.1.if else
void main() {
var score = 87;
if (score > 90) {
print('A');
} else if (score > 80) {
print('B');
} else if (score > 70) {
print('C');
} else if (score > 60) {
print('D');
} else {
print('E');
}
}
8.2.switch case
void main() {
var sex = '女';
switch (sex) {
case '男':
print('男');
break;
case '女':
print('女');
break;
default:
print('error');
}
}
8.3三目运算符
void main() {
bool flag = true; //三目运算符
String c = flag ? 'true' : 'false';
print(c);
}
8.4.??运算符
void main() {
var a;//??运算符
var b = a ?? 10;
print(b);
}
9.循环语句
9.1for
void main() {
for (int i = 0; i <= 50; i++) {
if (i % 2 == 0) {
print(i);
}
}
}
9.2while
void main() {
int i = 1;
while (i <= 10) {
print(i);
i++;
}
}
9.4.do while
void main() {
int i = 1;
while (i <= 10) {
print(i);
i++;
}
}
10.函数
10.1函数的命名
void printInfo() {
print('自定义');
}
void main() {
printInfo();
}
void main() {
int getNum() {
var mynum = 123;
return mynum;
}
var n = getNum();
print(n);
}
10.2调用方法传参
main() {
int sumNum(n) {
var sum = 0;
for (var i = 0; i <= n; i++) {
sum += i;
}
return sum;
}
var n1 = sumNum(60);
print(n1);
var n2 = sumNum(100);
print(n2);
}
10.3匿名方法
void main() {
var printNum = () {
print(123);
};
printNum();
}
10.4自执行方法
void main() {
(() {
print('我是自执行方法');
})();
}
10.5递归方法
void main() {
var sum = 1;
fn(int a) {
sum *= a;
if (a == 1) {
return;
}
fn(a - 1);
}
fn(5);
print(sum);
}
11.闭包
void main() {
fn() {
var a = 123;
return () {
a++;
print(a);
};
}
var b = fn();
b();
b();
b();
}
12.类
12.1自定义类
class Persion {
String name = 'Allce';
int age = 23;
void getInfo() {
print("$name------$age");
print("${this.name}------${this.age}");
}
void setInfo(int age) {
this.age = age;
}
}
12.2实例化
void main() {
var p1 = new Persion();
print(p1.name);
p1.getInfo();
Persion p2 = new Persion();
p2.getInfo();
Persion p3 = new Persion();
p3.setInfo(18);
p3.getInfo();
}
12.3默认构造函数
class Persion {
String name = 'Allce';
int age = 23;
Persion() {
print('构造函数');
}
void printInfo() {
print("${this.name}------${this.age}");
}
}
void main() {
Persion p4 = new Persion();
p4.printInfo();
}
12.4类抽离成文件
lei文件
class Person {
String name;
int age;
Person(this.name, this.age);
void printInfo() {
print("${this.name}------${this.age}");
}
}
class Animal {
String _name = 'Allce';
int age = 10;
Animal(this._name, this.age);
void printInfo() {
print('${this._name}-----${this.age}');
}
String getName() {
return this._name;
}
}
运行文件
import 'lib/lei.dart';
void main() {
Person p5 = new Person('Allce', 20);
p5.printInfo();
Person p6 = new Person('July', 28);
p6.printInfo();
}
12.5getter和setter
class Rect {
num height = 0;
num weight = 0;
Rect(this.height, this.weight);
get area {
return this.height * this.weight;
}
set areaHeight(value) {
this.height = value;
}
}
void main() {
Rect r = new Rect(10, 4);
print('面积:${r.area}');
r.areaHeight = 2;
r.area;
print('面积:${r.area}');
}
12.6类的继承
12.6.1继承父类
class Person {
String name = 'Allce';
int age = 20;
void printInfo() {
print("${this.name}------${this.age}");
}
}
class Web extends Person {}
main() {
Web w = new Web();
print(w.name);
w.printInfo();
}
12.6.2.扩展父类 super的使用
class Person {
String name;
int age;
Person(this.name, this.age);
Person.xxx(this.name, this.age);
void printInfo() {
print("${this.name}------${this.age}");
}
}
class Web extends Person {
String sex = 'man'; //不可为空?
Web(String name, int age, var sex) : super.xxx(name, age) {
//和视频不一样 super是把子类的参数赋值给父类
this.sex = sex;
}
run() {
print("${this.name}---${this.age}-----${this.sex}");
}
}
main() {
Web w = new Web('Allce', 20, 'woman');
w.printInfo();
w.run();
}
12.6.3覆写父类
class Person {
String name;
int age;
Person(this.name, this.age);
void printInfo() {
print("${this.name}------${this.age}");
}
work() {
print("${this.name}在工作。");
}
}
class Web extends Person {
Web(
String name,
int age,
) : super(name, age);
run() {
print('run');
super.work(); //
}
@override //覆写父类
void printInfo() {
print("姓名:${this.name}------年龄:${this.age}");
}
}
main() {
Web w = new Web('Allce', 20);
w.printInfo();
w.run();
}
13.静态方法和静态属性
class Person {
static String name = 'Allce';
int age = 20;
static void show() {
print(name);
}
void printInfo() {
print(name); //访问静态属性
print(this.age); //访问非静态属性
show(); //调用静态方法
}
static void printUserInfo() {
print(name);
show();
}
}
main() {
print(Person.name);
Person.show();
Person p = new Person();
p.printInfo();
Person.printUserInfo();
}
14.对象操作符
class Person {
String name;
int age;
Person(this.name, this.age);
void printInfo() {
print("${this.name}------${this.age}");
}
}
main() {
Person p = new Person('Seven', 18);
var p1;
p1 = new Person('Anny', 20);
(p1 as Person).printInfo();
Person p2 = new Person('Loki', 18);
p2.printInfo();
p2
..name = 'tore'
..age = 20
..printInfo();
}
15.抽象方法
15.1抽象类方法
abstract class Animal {
eat();
}
class Dog extends Animal {
@override
eat() {
print('小狗在吃肉骨头');
}
}
main() {
Dog d = new Dog();
d.eat();
}
15.2多态
abstract class Animal {
eat();
run();
}
class Dog extends Animal {
@override
eat() {
print('小狗在吃肉骨头');
}
@override
run() {
print('小狗在跑');
}
}
class Cat extends Animal {
@override
eat() {
print('小猫在吃鱼');
}
@override
run() {
print('小猫在跑');
}
}
main() {
Dog d = new Dog();
d.eat();
d.run();
Cat c = new Cat();
c.eat();
c.run();
Animal d_ = new Dog();
d_.eat();
}
16.接口
16.1.接口
abstract class DB {
String uri = 'seven';
add(data);
save();
delete();
}
class Mysql implements DB {
@override
String uri;
Mysql(this.uri);
@override
add(data) {
// TODO: implement add
print('Mysql方法' + data);
}
@override
delete() {
// TODO: implement delete
return null;
}
@override
save() {
// TODO: implement save
throw UnimplementedError();
}
}
class MsSql implements DB {
@override
add(data) {
// TODO: implement add
throw UnimplementedError();
}
@override
delete() {
// TODO: implement delete
throw UnimplementedError();
}
@override
save() {
// TODO: implement save
throw UnimplementedError();
}
@override
late String uri;
}
main() {
Mysql mysql = new Mysql('XXXX');
mysql.add('123');
}
16.2mixins
class Person {
late String name;
late int age;
Person(this.name, this.age);
printInfo() {
print('${this.name}-----${this.age}');
}
void run() {
print('person run');
}
}
mixin A {
//和视频不一样不能用class
String info = 'this is A';
void printA() {
print('A');
}
void run() {
print('a run');
}
}
mixin B {
void printB() {
print('B');
}
void run() {
print('b run');
}
}
class C extends Person with A, B {
C(String name, int age) : super(name, age);
}
void main() {
var c = new C('allce', 18);
c.printA();
c.printB();
print(c.info);
c.printInfo();
c.run();
print(c is A);
print(c is B);
}
16.3泛型
T getData<T>(T value) {
return value;
}
void main() {
print(getData<int>(12));
print(getData('hello'));
}
16.4泛型类
class MyList<T> {
List list = <T>[];
void add(T value) {
this.list.add(value);
}
List getList() {
return list;
}
}
main() {
MyList l1 = new MyList();
l1.add('elsa');
l1.add(12);
l1.add(true);
print(l1.getList());
}
16.5泛型接口
abstract class CaChe<T> {
getByKey(String key);
void setByKey(String key, T value);
}
class FileCaChe<T> implements CaChe<T> {
@override
getByKey(String key) {
// TODO: implement getByKey
throw UnimplementedError();
}
@override
void setByKey(String key, T value) {
print('key=${key} valur=${value}');
}
}
class MemoryCaChe<T> implements CaChe<T> {
@override
getByKey(String key) {
// TODO: implement getByKey
throw UnimplementedError();
}
@override
void setByKey(String key, T value) {
print('key=${key} valur=${value}');
}
}
void main() {
MemoryCaChe m = new MemoryCaChe();
m.setByKey('name', 'elsa');
}
17.库
import 'dart:io';
import 'dart:convert';
void main() async {
var result = await getDataFromZhihuAPI();
print(result);
}
getDataFromZhihuAPI() async {
var httpClient = new HttpClient();
var uri = new Uri.http('new-at.zhihu.com', '/api/3/stories/latest');
var request = await httpClient.getUrl(uri);
var reponse = await request.close();
return await reponse.transform(utf8.decoder).join();
}