// 长方形类
// let rr = new Rectangle(10,5);
// rr.setName('Rectangle1')
// rr.area ===> 50
// rr.width = 20
// rr.height = 10
// rr.area ===> 50 ? 想想为什么?
class Rectangle{
_width = 0;
_height = 0;
_name = '';
constructor(width,height){
this._width = width;
this._height = height;
}
setName(name){
this._name = name;
}
get area() {
return this._height*this._width;
}
set height(h){
if (h<10){
this._height = h;
}
}
set width(w){
if (w<10){
this._width = w;
}
}
log(){
console.log(`${this._name} info: width=${this._width} height=${this._height} area=${this.area}`)
}
}
// 正方形类
// let ss = new Square(8);
// ss.setName('Square1')
// ss.height = 6
// ss.area ===> 36
// ss.width = 7
// ss.area ===> 49
// ss.log() ===> Square Info:......
class Square extends Rectangle{
constructor(width){
super(width,width);
}
set height(h){
super.height = h;
super.width = h;
}
set width(w){
super.width = w;
super.height = w;
}
log(){
console.log('Squre Info:');
super.log();
}
}