一、构造函数

Dart实现了多种类型的构造函数。

1.1 生成构造函数

1
2
3
4
5
6
class Point {
double x;
double y;

Point(this.x, this.y);
}

1.2 默认构造函数

如果你不声明构造函数,Dart 会使用默认构造函数。默认构造函数是无参数且无名称的生成式构造函数。

1
2
3
4
class Point{

Point(){}
}

1.3 命名构造函数

子类不会继承超类的命名构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Person{
String name;
int age;

Person(this.name,this.age);

// 命名构造函数
Person.withDefaultAge(this.name):age = 18;


// 命名构造函数 从出生年份推算年龄
Person.fromBirthYear(this.name,int birthYear):age = DateTime.now().year - birthYear;

//命名构造函数:重定向
Person.anonymous():this("匿名",0);


void sayHi() => print('$name ,$age岁');
}


void main(){
var p1 = Person('张三', 25);
var p2 = Person.withDefaultAge('李四');
var p3 = Person.fromBirthYear('王五', 2000);
var p4 = Person.anonymous();


p1.sayHi();
p2.sayHi();
p3.sayHi();
p4.sayHi();

}

1.4 常量构造函数

如果你的类生成不变的实例,将这些实例定义为编译时常量

1
2
3
4
5
6
7
class ImmutablePoint {
static const ImmutablePoint origin = ImmutablePoint(0, 0);

final double x, y;

const ImmutablePoint(this.x, this.y);
}

1.5 重定向构造函数

一个构造函数可能重定向到同一类中的另一个构造函数。一个重定向构造函数的体为空

1
Point.alongXAxis(double x) : this(x, 0);

1.6 工厂构造函数

当遇到以下实现构造函数,使用factory

  • 构造函数并不总是创建其类的新实例。尽管工厂构造函数不能返回 null,但它可能会返回

    • 从缓存中返回一个现有实例而不是创建新实例
    • 返回一个子类型的全新实例
  • 在构造实例之前,你需要执行非平凡的准备工作。这可能包括检查参数或执行任何不能在初始化列表中处理的其它处理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
class Logger {
final String name;
bool mute = false;

// _cache is library-private, thanks to
// the _ in front of its name.
static final Map<String, Logger> _cache = <String, Logger>{};

factory Logger(String name) {
return _cache.putIfAbsent(name, () => Logger._internal(name));
}

factory Logger.fromJson(Map<String, Object> json) {
print(json);
return Logger(json['name'].toString());
}

Logger._internal(this.name);

void log(String msg) {
if (!mute) print(msg);
}
}

void main(){
Logger logger = Logger("张三");
print(logger);
print(Logger._cache);
}

工厂构造函数不能访问 this

二、实例化变量初始化

Dart可以通过三种方式初始化变量

2.1 在声明中初始化实例变量

1
2
3
4
5
6
7
8
9
10
11
12
class PointA {
double x = 1.0;
double y = 2.0;

// The implicit default constructor sets these variables to (1.0,2.0)
// PointA();

@override
String toString() {
return 'PointA($x,$y)';
}
}

2.2 使用初始化实例变量

2.3 使用初始化列表

三、构造函数继承

四、方法

方法是为对象提供行为的函数

4.1 实例方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import 'dart:math';

class Point {
final double x;
final double y;

// Sets the x and y instance variables
// before the constructor body runs.
Point(this.x, this.y);

double distanceTo(Point other) {
var dx = x - other.x;
var dy = y - other.y;
return sqrt(dx * dx + dy * dy);
}


4.2 操作符

4.3获取器和设置器

获取器和设置器是提供读和写功能的特殊方法 访问对象属性

1
2
3
4
5
6
7
8
9
10
11
12
13
class Rectangle{
double left,top,width,height;

Rectangle(this.left,this.top,this.width,this.height);

double get right => left + top;
set right(double value) => left = value - width;
}
void main(){

Rectangle r1 = Rectangle(10, 10, 100, 100);
print(r1.right);
}

4.4 抽象方法

抽象方法只能存在于抽象类混入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
abstract class Doer{

void doSomething();
}

class EffectiveDoer extends Doer {
void doSomething() {
print("请求体");
}
}


void main(){
EffectiveDoer e = EffectiveDoer();
e.doSomething();
}

本文作者:finyou

本文链接:http://finyou.top/2026/04/09/flutter/dart/class/

版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!

ESC 关闭 | 导航 | Enter 打开
输入关键词开始搜索