1/* 2 * Copyright (c) 2023 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 16declare function print(str:any):string; 17 18class A { 19 constructor() {} 20 21 foo?(): string; 22 23 bar(): string { 24 return "bar"; 25 } 26 27 bar2(): string { 28 return "bar2"; 29 } 30} 31 32let a = new A(); 33print(a.bar()); 34print(a.bar2()); 35print(Reflect.ownKeys(A.prototype)); //constructor,bar,bar2 36 37class A2 { 38 constructor() {} 39 40 foo?(): string; 41 foo(): string { 42 return "foo"; 43 } 44 45 bar(): string { 46 return "bar"; 47 } 48 49 bar2(): string { 50 return "bar2"; 51 } 52} 53 54let a2 = new A2(); 55print(a2.foo()); 56print(a2.bar()); 57print(a2.bar2()); 58print(Reflect.ownKeys(A2.prototype)); //constructor,foo,bar,bar2 59 60 61class B { 62 constructor() {} 63 64 bar(): string { 65 return "bar"; 66 } 67 68 foo?(): string; 69 70 bar2(): string { 71 return "bar2"; 72 } 73} 74 75let b = new B(); 76print(b.bar()); 77print(b.bar2()); 78print(Reflect.ownKeys(B.prototype)); //constructor,bar,bar2 79 80class B2 { 81 constructor() {} 82 83 bar(): string { 84 return "bar"; 85 } 86 87 foo?(): string; 88 foo(): string { 89 return "foo"; 90 } 91 92 bar2(): string { 93 return "bar2"; 94 } 95} 96 97let b2 = new B2(); 98print(b2.foo()); 99print(b2.bar()); 100print(b2.bar2()); 101print(Reflect.ownKeys(B2.prototype)); //constructor,bar,foo,bar2 102 103// one signature but no body 104class C { 105 constructor() {} 106 107 foo?(): string; 108 109 bar(): string { 110 return "test one signature but no body"; 111 } 112} 113 114let c = new C(); 115print(c.bar()); 116 117// multi-signatures but one body 118class D { 119 constructor() {} 120 121 foo?(a: string): string; 122 123 foo?(a: string): string { 124 return a; 125 } 126 127 foo?(a: string, b?: string): string; 128 129 bar(): string { 130 return "test multi-signatures but one body"; 131 } 132} 133 134let d = new D(); 135print(d.foo!("D")); 136print(d.bar()); 137 138// multi-signature but no body. 139class E { 140 constructor() {} 141 142 foo?(): string; 143 144 foo?(a: string): string; 145 146 foo?(a: string, b: string): string; 147 148 bar(): string { 149 return "test multi-signatures but no body"; 150 } 151} 152 153E.prototype.foo = function(): string { 154 return "E"; 155} 156 157let e = new E(); 158print(e.foo!()); 159print(e.bar()); 160 161