Skip to content

Commit b81dd59

Browse files
author
yinhuasheng
committed
variables & operator
1 parent a526a14 commit b81dd59

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

basic/libraries.dart

Whitespace-only changes.

basic/metadata.dart

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
void main() {
2+
var tom = Person("Tom", 12);
3+
tom.sayHello();
4+
tom.sayHi();
5+
}
6+
7+
class Person {
8+
String name;
9+
int age;
10+
Person(this.name, this.age);
11+
12+
@Deprecated("use sayHi")
13+
void sayHello() {
14+
print("hello $name");
15+
}
16+
17+
@MyMeta("test", "test my meta date")
18+
void sayHi() {
19+
print("Hi $name");
20+
}
21+
}
22+
23+
//dart的注解很简洁 一个class就可以作为注解
24+
class MyMeta {
25+
final String who;
26+
final String what;
27+
28+
const MyMeta(this.who, this.what);
29+
}

basic/operator.dart

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
void main() {
2+
const a = 10;
3+
const b = 3;
4+
// dart的/是除,但会保留小数,返回类型是double 这与大部分编程语言不同
5+
const c = a / b; //3.333333
6+
print(c);
7+
// ~/对应的是整除,只保留整数部分
8+
const d = a ~/ b; //3
9+
print(d);
10+
11+
var point1 = Point(1, 3);
12+
var point2 = Point(1, 3);
13+
print(point1 == point2); //true
14+
15+
// ??=运算符 a ??= b等价于 if(a == null){a == b} else{ do nothing}
16+
var a1 = 12;
17+
int? b1 = null;
18+
b1 ??= a1;
19+
var c1 = 13;
20+
c1 ??= a1;
21+
print(b1);
22+
print(c1);
23+
24+
//??=运算符其实就是??加上赋值运算符=
25+
//下面的expr1 ?? expr2 如果 expr1 非空,则返回其值;否则,计算并返回 expr2 的值。
26+
//我们可以将dart的??理解为if null表达式
27+
var result1 = point1.x ?? point1.y;
28+
29+
//级联运算符 这要用于函数式编程的写法,如:
30+
var point3 = Point(11, 12)
31+
..x = 1
32+
..printIt()
33+
..y = 2
34+
..printIt();
35+
36+
//spread 展开运算符 主要用于集合,将一个几个展开插入到另一个集合中
37+
}
38+
39+
//dart支持运算符重载 这个功能很棒
40+
class Point {
41+
int? x;
42+
int y;
43+
44+
Point(this.x, this.y);
45+
46+
@override
47+
bool operator ==(Object other) {
48+
//类型检测/转换 as 是将对象转为某一类型,is 和is!是类型判断
49+
return other is Point && this.x == other.x && this.y == other.y;
50+
}
51+
52+
//重写 == 一定也要重写hash 避免用到hash的场景
53+
@override
54+
int get hashCode => Object.hash(this.x, this.y);
55+
56+
void printIt() {
57+
print('x:$x,y:$y');
58+
}
59+
}

basic/variables.dart

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import '../start/main.dart';
2+
3+
void main() {
4+
var person1 = Person("Tom", 12);
5+
var person2 = Person("Lee", null);
6+
print(person1);
7+
print(person2);
8+
9+
var even1 = person1.age?.isEven;
10+
print(even1); //true
11+
var even2 = person2.age?.isEven;
12+
print(even2); //null
13+
14+
var student = new Student();
15+
student.setName("Jerry");
16+
student.sayHello();
17+
}
18+
19+
class Person {
20+
String name;
21+
int? age;
22+
23+
Person(this.name, this.age);
24+
25+
@override
26+
String toString() {
27+
return 'name:$name,age:$age';
28+
}
29+
}
30+
31+
class Student {
32+
late String name;
33+
void setName(String name) {
34+
this.name = name;
35+
}
36+
37+
void sayHello() {
38+
print("Hello $name");
39+
}
40+
}

0 commit comments

Comments
 (0)