class People {
constructor(name, age) { //构造函数的属性
this.name = name;
this.age = age;
this._sex = -1
}
get sex() { //顶层属性 get只读
if (this._sex === 1) {
return 'man';
} else if (this._sex === 0) {
return 'woman';
} else {
return 'error';
}
}
set sex(val) { //顶层属性 set
if (val === 0 || val === 1) {
this._sex = val;
}
}
showName() { //方法
console.log(this.name);
}
static getCount() { //静态方法
return 5
}
}
let p1 = new People('kiw', 18);
console.log('p1', p1); //{age: 18, name: "kiw", _sex: 0}
console.log('p1.sex', p1.sex); //error
p1.sex = 0;
console.log('p1.sex', p1.sex); //woman
p1.showName(); // kiw
People.getCount();
class People {
constructor(name, age) { //构造函数的属性
this.name = name;
this.age = age;
}
showName() { //方法
console.log(this.name);
}
}
class Coder extends People {
constructor(name, age, company) {
super(name, age);
this.company = company;
}
showCompany() {
console.log(this.company);
}
}
let c1 = new Coder('zhangsan', 22, 'imooc');
console.log('c1', c1); //Coder {name: "zhangsan", age: 22, company: "imooc"}
c1.showName(); // zhangsan
c1.showCompany(); //imooc
网友评论