为了账号安全,请及时绑定邮箱和手机立即绑定

类中扩展类的使用方法

类中扩展类的使用方法

慕娘9325324 2021-05-19 16:04:43
我正在尝试在扩展类中使用扩展类的方法(这可能不是很清楚,但是请看下面的示例,您将会理解)。我的模块是用打字稿制作的:export default class Test1 {  private storedCond: string;  public cond = (cond: string) => {    this.storedCond = cond;    return this.storedCond.length;  };  public getStoredCond = () => this.storedCond;}然后我想使用的是js文件中的某些方式:import Test1 from './modules/Test1';class Test2 extends Test1 {  // this line is not working, how can i make it work ?  this.cond('a futur dynamic string');  final = () => `${this.getStoredCond()} is ${this.getStoredCond().length} length`;}const test = new Test2();console.log(test.final());我的Test1模块有点像商店,我可以做这样的事情:sotredCond = this.cond('a futur dynamic string');在我的Test2课上,但这不是我想要做的。我想cond在我的Test2课程中提供一个字符串()并将其存储在我的Test1模块中。
查看完整描述

2 回答

?
守着星空守着你

TA贡献1799条经验 获得超8个赞

我的Test2类的最终代码是:


class Test2 extends Test1 {

  constructor() {

    super();

    this.cond('a futur dynamic string');

  }


  final = () =>

    `${this.getStoredCond()} is ${this.getStoredCond().length} length`;

}


查看完整回答
反对 回复 2021-05-27
  • 2 回答
  • 0 关注
  • 150 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信