HarmonyOS 如何给某个系统类,如String添加扩展?

ArkTS现在是否可以为String类添加扩展,以链式语法实现如下自定义方法 xxxfunction 吗?

例:

a: string = 'abcdefg',
a.xxxfuntion()
阅读 463
1 个回答

ArkTS没有原型的概念

目前对于拓展的相关述求,只能通过继承基类的方式进行扩展。

1、定义一个基类,包含需要扩展的方法。

class Base {
  method1() {
    console.log('method1 from Base');
  }
}

2、定义一个继承自基类的子类,重写需要扩展的方法,并在子类中添加新的方法。

class Child extends Base {
  method1() {
    console.log('method1 from Child');
  }

  method2() {
    console.log('method2 from Child');
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进