Skip to content
章节导航

Dart 的 get 和 set 方法

Get

可以为私有字段设置 getter 来让外界获取到私有字段,例如:

shell
String? get school => _school;

使用方法

shell
String school = student.school;
print('school: $school');

Set

可以为私有字段设置 setter 来控制外界对私有字段的修改,例如:

shell
set school(String? value) {
    _school = value;
}

使用方法

shell
student.school = "985";