类
定义、使用类
定义
class Point {
}
使用
var p = Point();
构造函数
定义
class Point {
num x, y;
Point(this.x, this.y);
@override
String toString() {
return "$x, $y";
}
}
使用
var p = Point(1, 2);
print(p);
1, 2
初始化列表
定义
class Point {
num x, y;
Map origin1, origin2;
Point(this.x, this.y)
: origin1 = {'x': x, 'y': y},
origin2 = {'x': x + 10, 'y': y + 10};
}
使用
void main(List<String> args) {
var p = Point(1, 2);
print(p);
1, 2, {1: 1, 2: 2}, {1: 2, 2: 4}
命名构造函数
定义
class Point {
num x, y;
Map origin1, origin2;
Point.fromJson(Map json)
: x = json['x'],
y = json['y'],
origin1 = {'x': json['x'], 'y': json['y']},
origin2 = {'x': json['x'] + 10, 'y': json['y'] + 10};
}
使用
var p = Point.fromJson({"x": 1, "y": 2});
print(p);
10, 20, {x: 10, y: 20}, {x: 20, y: 30}
重定向构造函数
定义
class Point {
num x, y;
Map origin1, origin2;
Point(this.x, this.y)
: origin1 = {'x': x, 'y': y},
origin2 = {'x': x + 10, 'y': y + 10};
// 重定向构造函数
Point.fromJson(Map json) : this(json['x'], json['y']);
}
使用
var p = Point.fromJson({"x": 1, "y": 2});
print(p);
10, 20, {x: 10, y: 20}, {x: 20, y: 30}
callable
class IOSPhone {
call(String num) {
print('phone number is $num');
}
}
main(List<String> args) {
var phone = IOSPhone();
phone('911');
}
phone number is 911
get set
定义、使用 get set
定义
class People {
String? _name;
People();
set name(String value) {
_name = value;
}
String get name {
return 'people is $_name';
}
}
使用
var p = People();
p.name = 'ducafecat';
print(p.name);
people is ducafecat
简化 get set
class People {
String? _name;
People();
set name(String value) => _name = value;
String get name => 'people is $_name';
}
业务场景
购物车
/// 商品数量
int get lineItemsCount => lineItems.length;
/// 运费
double get shipping => 0;
/// 折扣
double get discount =>
lineCoupons.fold<double>(0, (double previousValue, CouponsModel element) {
return previousValue + (double.parse(element.amount ?? "0"));
});
/// 商品合计价格
double get totalItemsPrice =>
lineItems.fold<double>(0, (double previousValue, LineItem element) {
return previousValue + double.parse(element.total ?? "0");
});
以前可能会写个方法
getXXX()当然也适用于赋值操作
static
静态变量
static 定义
声明
class People {
static String name = 'ducafecat';
}
调用
静态变量可以通过外部直接访问,不需要将类实例化
print(People.name);
ducafecat
函数内部访问
实例化后的类也可以访问该静态变量
声明
class People {
static String name = 'ducafecat';
void show() {
print(name);
}
}
调用
People().show();
ducafecat
静态方法
静态方法可以通过外部直接访问
声明
class People {
static String name = 'ducafecat';
static void printName() {
print(name);
}
}
调用
People.printName();
ducafecat
业务场景
EasyLoading 用大量的 static 来方便方法调用
抽象
abstract 类、函数、成员
- 普通类前加 abstract
abstract class Person {
String name = 'ducafecat';
void printName() {
print(name);
}
}
不能直接 new 实例化
var p = Person();
p.printName();
Abstract classes can't be instantiated.
Try creating an instance of a concrete subtype.
继承方式使用
class Teacher extends Person {
}
var p = Teacher();
p.printName();
ducafecat
接口方式使用
定义
abstract class Person {
String name = '';
void printName();
}
class Teacher implements Person {
@override
String name;
Teacher(this.name);
@override
void printName() {
print('Teacher: $name');
}
}
实例
var p = Teacher("ducafecat");
p.printName();
ducafecat
接口
- Dart 没有接口
接口方式使用
- 定义人抽象类
abstract class IPerson {
String name;
int age;
IPerson(this.name, this.age);
String info() {
return 'Name: $name, Age: $age';
}
}
接口用途的抽象类 请用字母
I开头 , 如IPhone
- 老师
class Teacher implements IPerson {
@override
String name;
@override
int age;
Teacher(this.name, this.age);
@override
String info() {
return 'Teacher -> Name: $name, Age: $age';
}
}
- 学生
class Student implements IPerson {
@override
int age;
@override
String name;
Student(this.name, this.age);
@override
String info() {
return 'Student -> Name: $name, Age: $age';
}
}
- 打印信息
void makePersonInfo(IPerson user) => print(user.info());
- 实例化
void main(List<String> args) {
var t = Teacher('ducafecat', 99);
makePersonInfo(t);
var s = Student('hans', 66);
makePersonInfo(s);
}
Teacher -> Name: ducafecat, Age: 99
Student -> Name: hans, Age: 66
履行多接口
- 定义学校抽象类
abstract class ISchool {
int grade;
ISchool(this.grade);
String schoolInfo() {
return 'Grade: $grade';
}
}
- 学生 多继承
class Student implements IPerson, ISchool {
@override
int age;
@override
String name;
@override
int grade;
Student(this.name, this.age, this.grade);
@override
String info() {
return 'Student -> Name: $name, Age: $age';
}
@override
String schoolInfo() {
return 'School -> Name: $name, Age: $age, Grade: $grade';
}
}
- 打印信息
void makePersonInfo(IPerson user) => print(user.info());
void makeSchoolInfo(ISchool user) => print(user.schoolInfo());
- 实例化
void main(List<String> args) {
var t = Teacher('ducafecat', 99);
makePersonInfo(t);
var s = Student('hans', 66, 5);
makePersonInfo(s);
makeSchoolInfo(s);
}
Teacher -> Name: ducafecat, Age: 99
Student -> Name: hans, Age: 66
School -> Name: hans, Age: 66, Grade: 5
从一个普通类履行接口
class Phone {
void startup() {
print('开机');
}
void shutdown() {
print('关机');
}
}
class AndroidPhone implements Phone {
@override
void startup() {
print('AndroidPhone 开机');
}
@override
void shutdown() {
print('AndroidPhone 关机');
}
}
void main() {
var p = AndroidPhone();
p.startup();
p.shutdown();
}
Dart 可以从一个普通的类履行接口