• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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/*---
17 desc: 10.04.Interface_Members
18 name: 10.Interfaces/10.04.Interface_Members/interface_with_optional_attributes_0003.sts
19 tags:
20 ---*/
21
22interface IStudent {
23  name: string;
24  age: int;
25  desc?: string | undefined;
26  teacher?: Teacher | undefined;
27}
28
29class Teacher {
30  name: string;
31
32  constructor(name: string) {
33    this.name = name;
34  }
35}
36
37class Student implements IStudent {
38  name: string;
39  age: int;
40  desc?: string | undefined;
41  teacher?: Teacher | undefined;
42
43  constructor(name: string, age: int, desc: string, teacher: Teacher) {
44    this.name = name;
45    this.age = age;
46    this.desc = desc
47    this.teacher = teacher
48  }
49
50  constructor(name: string, age: int) {
51    this.name = name;
52    this.age = age;
53  }
54
55  constructor(name: string, age: int, desc: string) {
56    this.name = name;
57    this.age = age;
58    this.desc = desc;
59  }
60
61  constructor(name: string, age: int, teacher: Teacher) {
62    this.name = name;
63    this.age = age;
64    this.teacher = teacher;
65  }
66}
67
68function main() : void {
69  let s0 : Student = new Student("Klara", 18, "an excellent student", new Teacher("Anna"));
70  let s1 : Student = new Student("Klara", 18);
71  let s2 : Student = new Student("Klara", 18, "an excellent student");
72  let s3 : Student = new Student("Klara", 18,  new Teacher("Anna"));
73
74  assert s0.name == "Klara"
75  assert s0.age == 18
76  assert s0.desc == "an excellent student"
77  assert s0.teacher?.name == "Anna"
78
79  assert s1.name == "Klara"
80  assert s1.age == 18
81  assert s1.desc == undefined
82  assert s1.teacher?.name ==undefined
83
84  assert s2.name == "Klara"
85  assert s2.age == 18
86  assert s2.desc == "an excellent student"
87  assert s2.teacher?.name == undefined
88
89  assert s3.name == "Klara"
90  assert s3.age == 18
91  assert s3.desc == undefined
92  assert s3.teacher?.name == "Anna"
93}
94