• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/**---
16 description: >
17  TypeScript allows us to mark a class as abstract. This tells TypeScript that the class is only meant to be extended from, and that certain members need to be filled in by any subclass to actually create an instance.
18 module: ESNext
19 isCurrent: true
20---*/
21
22
23import { Assert } from '../../../suite/assert.js'
24
25abstract class Color {
26    abstract red: number;
27    abstract green: number;
28    abstract blue: number;
29    protected abstract rgb: [number, number, number];
30
31    abstract getRGB(): [number, number, number];
32}
33
34class ColorCopy extends Color {
35    red: number = 0;
36    green: number = 0;
37    blue: number = 0;
38    protected rgb: [number, number, number] = [0, 0, 0];
39    getRGB(): [number, number, number] {
40        this.rgb = [this.red, this.green, this.blue]
41        return this.rgb;
42    }
43    constructor(r: number, g: number, b: number) {
44        super();
45        this.red = r;
46        this.green = g;
47        this.blue = b;
48        this.getRGB();
49    }
50}
51
52let color: ColorCopy = new ColorCopy(255, 0, 0);
53Assert.equal(JSON.stringify(color.getRGB()), '[255,0,0]');