1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import { webview } from '@kit.ArkWeb'; 17import { AbilityLifecycleCallback, UIAbility } from '@kit.AbilityKit'; 18 19class OptionalMethod implements webview.NativeMediaPlayerBridge { 20 updateRect(x: number, y: number, width: number, height: number): void { 21 throw new Error('Method not implemented.'); 22 } 23 24 play(): void { 25 throw new Error('Method not implemented.'); 26 } 27 28 pause(): void { 29 throw new Error('Method not implemented.'); 30 } 31 32 seek(targetTime: number): void { 33 throw new Error('Method not implemented.'); 34 } 35 36 setVolume(volume: number): void { 37 throw new Error('Method not implemented.'); 38 } 39 40 setMuted(muted: boolean): void { 41 throw new Error('Method not implemented.'); 42 } 43 44 setPlaybackRate(playbackRate: number): void { 45 throw new Error('Method not implemented.'); 46 } 47 48 release(): void { 49 throw new Error('Method not implemented.'); 50 } 51 52 enterFullscreen(): void { 53 throw new Error('Method not implemented.'); 54 } 55 56 exitFullscreen(): void { 57 throw new Error('Method not implemented.'); 58 } 59 60 resumePlayer?(): void { // Error 61 throw new Error('Method not implemented.'); 62 } 63 64 suspendPlayer?(type: webview.SuspendType): void { // Error 65 throw new Error('Method not implemented.'); 66 } 67} 68 69class MyAbilityLifecycleCallback extends AbilityLifecycleCallback { 70 // 用例1: 重写为必选方法 预期报错 用例结果--Error 未识别 71 onWillNewWant(ability: UIAbility) { 72 console.log('AbilityLifecycleCallback onWillNewWant.'); 73 } 74 75 // 用例2: 重写为可选方法 预期报错 用例结果--Pass 76 onAbilityWillCreate?(ability: UIAbility) { 77 console.log('AbilityLifecycleCallback onAbilityWillCreate.'); 78 } 79 80 // 用例3:使用必选属性重写父类方法 预期无报错 用例结果--Pass 81 onAbilityWillForeground: (ability: UIAbility) => void = 82 (ability: UIAbility) => { 83 console.log('AbilityLifecycleCallback onAbilityWillForeground'); 84 } 85 // 用例4:使用可选属性重写父类方法 预期无报错 用例结果--Pass 86 onAbilityWillBackground?: (ability: UIAbility) => void = 87 (ability: UIAbility) => { 88 console.log('AbilityLifecycleCallback onAbilityWillBackground'); 89 } 90} 91 92// 用例5 类型断言 预期无报错 用例结果--Pass 93const callback: AbilityLifecycleCallback = { 94 onAbilityWillForeground: (ability: UIAbility): void => { 95 console.log('AbilityLifecycleCallback onAbilityWillForeground'); 96 } 97} as AbilityLifecycleCallback; 98 99// 用例6 泛型类拓展 100class GenericHandler<T extends UIAbility> extends AbilityLifecycleCallback { 101 // 预期报错 用例结果--Error 未识别 102 onAbilityWillCreate(ability: T): void { /*...*/ } 103 // 预期无报错 用例结果--Pass 104 onAbilityWillForeground: (ability: T) => void = 105 (ability: T) => { 106 console.log('AbilityLifecycleCallback onAbilityWillForeground'); 107 } 108} 109 110// 接口不能继承类;不能使用交叉类型扩展;不能声明合并 111 112 113