Dart笔记(16):get set

get set

定义

class People {
  String _name;

  set pName(String value) {
    _name = value;
  }

  String get pName {
    return 'people is ${_name}';
  }
}

使用

var p = new People();
p.pName = 'hxl';
print(p.pName); //打印 people is hxl

简化get set

class People {
  String _name;

  set pName(String value) => _name = value;

  String get pName => 'people is ${_name}';
}

你可能感兴趣的:(Dart笔记(16):get set)