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 16// @ts-nocheck 17 18// 多态 19class Base { 20 baseNum: number = 1 21 constructor(num: number){ 22 this.baseNum = num 23 } 24 compute(): number { 25 return this.baseNum 26 } 27} 28 29class DeriveDouble extends Base { 30 constructor(num: number){ 31 super(num); 32 } 33 compute() : number { 34 return this.baseNum * 2 35 } 36} 37 38class DerivedTripple extends Base { 39 constructor(num: number){ 40 super(num); 41 } 42 compute() : number { 43 return this.baseNum * 3 44 } 45} 46 47 48function Polymorphism() { 49 let count = 100000; 50 let result: number[] = new Array(); 51 let result2: Base[] = new Array(); 52 let result3: Base[] = new Array(); 53 for (let i = 0; i < count; i++) { 54 result.push(i); 55 result2.push(new DeriveDouble(i)); 56 result3.push(new DerivedTripple(i)); 57 } 58 for (let i = 0; i < count; i++) { 59 if (result[i] == i) { 60 result2[i].baseNum = result2[i].compute(); 61 result3[i].baseNum = result3[i].compute(); 62 } 63 } 64 let res:boolean = true 65 for (let i = 0; i < count; i++) { 66 if (result2[i].baseNum != i * 2 || result3[i].baseNum != i * 3) { 67 res = false 68 } 69 } 70 if (!res) { 71 print("result is wrong") 72 } 73} 74let loopCountForPreheat = 1; 75 76for (let i = 0; i < loopCountForPreheat; i++) { 77 Polymorphism() 78} 79 80ArkTools.waitAllJitCompileFinish(); 81 82Polymorphism() 83