Dart中类的getter和setter修饰符的用法
如果您想通过视频方式学习Dart中类的getter和setter修饰符的用法,请访问:https://www.itying.com/goods-1101.html
class Rect{
int height;
int width;
getArea(){
return this.height*this.width;
}
}
class Rect{
num height;
num width;
Rect(this.height,this.width);
area(){
return this.height*this.width;
}
}
void main(){
Rect r=new Rect(10,4);
print("面积:${r.area()}");
}
Dart中类的getter
class Rect{
num height;
num width;
Rect(this.height,this.width);
get area{
return this.height*this.width;
}
}
void main(){
Rect r=new Rect(10,2);
print("面积:${r.area}"); //注意调用直接通过访问属性的方式访问area
}
Dart中类的setter
class Rect{
num height;
num width;
Rect(this.height,this.width);
get area{
return this.height*this.width;
}
set areaHeight(value){
this.height=value;
}
}
void main(){
Rect r=new Rect(10,4);
// print("面积:${r.area()}");
r.areaHeight=6;
print(r.area);
}