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 16interface ZComparable<T> { 17 compare(other: T): int; 18} 19 20interface XComparable<T, U> extends ZComparable<U> { 21 compareTo(other: T): int; 22} 23 24abstract class YComparable<T> { 25 abstract compareFrom(other: T): int; 26} 27 28class V extends YComparable<V> implements XComparable<V, V> { 29 v: int; 30 31 constructor(v: int) { this.v = v; } 32 33 override compareTo(other: V) { 34 return this.v - other.v; 35 } 36 37 override compareFrom(other: V) { 38 return other.v - this.v; 39 } 40 41 override compare(other: V) { 42 return 2 * other.v - this.v; 43 } 44 45} 46 47function main() { 48 let v1 = new V(1) as XComparable<V, V>; 49 let v2 = new V(1) as YComparable<V>; 50 51 let v3 = new V(2); 52 53 assertEQ(v1.compareTo(v3), -1); 54 assertEQ(v3.compareTo(v1 as V), 1); 55 56 assertEQ(v1.compare(v3), 3); 57 assertEQ(v3.compare(v1 as V), 0); 58 59 assertEQ(v2.compareFrom(v3), 1); 60 assertEQ(v3.compareFrom(v2 as V), -1); 61} 62