HarmonyOS NEXT】鸿蒙中Interface实例实现的书写格式
·
在 HarmonyOS NEXT 的 ArkTS 开发中,Interface(接口)扮演着定义“契约”的角色,它只声明属性和方法的类型,而不提供具体实现。真正让接口“活”起来,是靠 class(类)或 struct(结构体) 通过 implements 关键字来实现的。
重要说明:ArkTS 基于 TypeScript,因此其接口实现语法与 TypeScript 基本一致。以下所有示例均基于 ArkTS / TypeScript 语法。
1. 核心语法:class 实现单接口
这是最基础、最常用的实现方式。类必须实现接口中声明的所有属性和方法,否则编译会报错。
typescript
// 1. 定义接口:行为的契约
interface AreaSize {
color: string; // 属性声明
calculateAreaSize(): number; // 方法声明
someMethod(): void;
}
// 2. 实现接口:类遵循契约
class RectangleSize implements AreaSize {
// 必须实现接口中的属性
color: string;
width: number;
height: number;
constructor(color: string, width: number, height: number) {
this.color = color;
this.width = width;
this.height = height;
}
// 必须实现接口中的方法
someMethod(): void {
console.log('计算面积前的准备工作');
}
calculateAreaSize(): number {
this.someMethod();
return this.width * this.height;
}
}
// 使用示例
const rect = new RectangleSize('红色', 10, 20);
console.log(rect.calculateAreaSize()); // 输出面积
2. 灵活实现:属性使用 getter/setter
接口声明属性时,既可以写成简单字段,也可以写成 getter/setter 对,两者在接口层面是等价的。实现类可以根据需要灵活选择。
typescript
interface Style {
color: string; // 声明方式一:简单字段
// 等效于声明方式二:getter/setter 对
// get color(): string
// set color(x: string)
}
// 实现方式一:直接用简单属性
class StyledRectangleSimple implements Style {
color: string = '';
}
// 实现方式二:用 getter/setter 进行逻辑控制
class StyledRectangleAdvanced implements Style {
private _color: string = '';
get color(): string {
// 可以在获取前做一些处理,比如格式化
return this._color.toUpperCase();
}
set color(x: string) {
// 可以在设置前做一些校验
if (x && x.length > 0) {
this._color = x;
}
}
}
3. 能力扩展:接口继承
一个接口可以继承另一个接口,从而拥有父接口的全部能力,并添加自己的新成员。实现子接口的类,必须实现整个继承链上的所有成员。
typescript
interface Style {
color: string;
}
// ExtendedStyle 继承了 Style,并添加了 width 属性
interface ExtendedStyle extends Style {
width: number;
}
// Cat 类必须实现 color 和 width 两个属性
class Cat implements ExtendedStyle {
color: string;
width: number;
constructor(color: string, width: number) {
this.color = color;
this.width = width;
}
}
4. 实战应用:在 UI 开发中定义数据模型
在 ArkUI 的声明式开发中,Interface 最常用来约束数据对象的类型,让代码更健壮,IDE 提示也更友好。
typescript
// 定义粉丝列表项的数据结构
interface FansItem {
avatar: Resource; // 头像资源
name: string; // 名称
title: string; // 标题
isFollow: boolean; // 是否已关注
}
@Entry
@Component
struct TestPage {
// 使用 Interface 约束数组类型
playerList: FansItem[] = [
{
name: '华为终端',
avatar: $r('app.media.flower'),
title: '2024,二百万粉阿华继续冲压!!!',
isFollow: false
},
{
name: '黑马程序员',
avatar: $r('app.media.flower'),
title: '领取课程源码+资料',
isFollow: true
}
];
build() {
List() {
ForEach(this.playerList, (item: FansItem) => {
ListItem() {
// ... 渲染 UI
Text(item.name).fontSize(16)
if (item.isFollow) {
Button('已关注')
} else {
Button('回关')
}
}
})
}
}
}
5. 进阶补充:Interface 与 Class 的对比
理解这两者的区别,能帮你做出更合理的设计决策:
| 特性 | Interface(接口) | Class(类) |
|---|---|---|
| 角色 | 定义类型约束的“契约/蓝图” | 创建具体对象的“模板/生产车间” |
| 内容 | 只能包含属性和方法的类型声明 | 可以包含属性、方法的具体实现代码 |
| 实例化 | 不能使用 new 实例化 |
可以使用 new 创建对象实例 |
| 关键词 | interface |
class,用 implements 实现接口 |
要点总结:
class通过implements关键字来实现interface。实现类必须完全实现接口中声明的所有属性和方法。
接口可以通过
extends继承其他接口,实现能力的组合与扩展。在 ArkUI 开发中,常用 Interface 来约束组件状态或列表数据,提升代码的可靠性。
更多推荐



所有评论(0)