【发布时间】:2022-10-24 08:02:21
【问题描述】:
我正在使用类创建一个子对象,但我不希望它从父对象继承某些属性和方法。
我想知道是否有任何方法可以做到这一点。
我的代码:
class Player {
#name;
#marking;
#score;
constructor(){
this.#name = undefined;
this.#marking = undefined;
this.#score = {wins:0,defeats:0};
}
action(){...}
getName(){...}
setName(){...}
...
}
class AIPlayer extends Player{
constructor(){
super();
this.#name = "AI-0.1.2";
}
action(){...}
//I don't want AIPlayer to inherit setName() or #score
}
const p1 = new Player();
p1.setName("Mr.Banana);
console.log(p1.getName()); //-> Mr.Banana
const AIP0 = new AIPlayer();
AIP0.setName("stupid computer"); //->error
console.log(AIP0.getName()); //-> AI-0.1.2
【问题讨论】:
标签: javascript class object methods properties