HarmonyOS ArkTS中如何判断某个类的实例是否实现了某个方法

HarmonyOS
2024-12-23 14:44:19
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

示例参考如下:

function getMethods(classInstance: ESObject): string[] {
  return Object.getOwnPropertyNames(classInstance.prototype).filter(name =>
  typeof classInstance.prototype[name] === 'function' && name !== 'constructor');
}
class FuncObj {
  constructor() {
  }
  func0() {
    console.log('func0')
  }
  public func1() {
    console.log('func1');
  }
  private func2() {
    console.log('func2');
  }
  protected func3() {
    console.log('func3')
  }
}
@Entry
@Component
struct Index {
  @State funcNames: string = ''

  build() {
    Column() {
      Button('获取函数名称').onClick(() => {
        const methodList = getMethods(FuncObj) as string[]
        this.funcNames = methodList.join('、')
      }).margin({ bottom: 20 })
      Text(this.funcNames)
    }.justifyContent(FlexAlign.Center)
    .alignItems(HorizontalAlign.Center)
    .height('100%')
    .width('100%')
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
分享
微博
QQ
微信
回复
2024-12-23 18:25:20


相关问题
如何判断一个对象实现某个接口
2408浏览 • 1回复 待解决
HarmonyOS判断Object是否含有某个属性
2220浏览 • 1回复 待解决
如何判断JS对象是否存在某个
3118浏览 • 1回复 待解决
如何判断对象是某个interface实现
1807浏览 • 1回复 待解决
怎么判断某个customDialog是否弹出?
893浏览 • 1回复 待解决
判断是否实现某接口
1085浏览 • 1回复 待解决
HarmonyOS 判断设备是否安装某个app
697浏览 • 1回复 待解决
如何判断某个应用是否为系统应用
3395浏览 • 1回复 待解决
HarmonyOS 如何通过反射创建某个
863浏览 • 1回复 待解决