判断类是否实现了某接口

判断类是否实现了某接口

HarmonyOS
2024-07-19 10:25:55
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
平方厘米

示例代码

interface A {
name: string;
}

class MyClass implements A {
name: string;

constructor(name: string) {
this.name = name;
}
}

let myObject: A = new MyClass('example');

if (myObject instanceof MyClass) {
let myInstance = myObject as MyClass;
console.log('myObject is an instance of MyClass');
console.log(myInstance.name);
} else {
console.log('myObject is not an instance of MyClass');
}
分享
微博
QQ
微信
回复
2024-07-19 19:36:35