|
1 | 1 | export class Shape { |
| 2 | + #x; |
| 3 | + #y; |
| 4 | + #col; |
| 5 | + |
2 | 6 | constructor(x, y, col) { |
3 | | - this._x = x; |
4 | | - this._y = y; |
5 | | - this._col = col; |
| 7 | + this.#x = x; |
| 8 | + this.#y = y; |
| 9 | + this.#col = col; |
6 | 10 | } |
7 | 11 |
|
8 | 12 | get x() { |
9 | | - return this._x; |
| 13 | + return this.#x; |
10 | 14 | } |
11 | 15 |
|
12 | 16 | set x(val) { |
13 | 17 | if (typeof val !== 'number') { |
14 | 18 | throw new TypeError('x must be a number'); |
15 | 19 | } |
16 | | - this._x = val; |
| 20 | + this.#x = val; |
| 21 | + } |
| 22 | + |
| 23 | + get y() { |
| 24 | + return this.#y; |
| 25 | + } |
| 26 | + |
| 27 | + get col() { |
| 28 | + return this.#col; |
17 | 29 | } |
18 | 30 |
|
19 | 31 | get area() { |
20 | 32 | throw new Error('area is not a property of this shape'); |
21 | 33 | } |
22 | 34 |
|
23 | 35 | moveBy(x, y) { |
24 | | - this._x += x; |
25 | | - this._y += y; |
| 36 | + this.#x += x; |
| 37 | + this.#y += y; |
26 | 38 | } |
27 | 39 | } |
28 | 40 |
|
29 | 41 | export class Rectangle extends Shape { |
| 42 | + #width; |
| 43 | + #height; |
| 44 | + |
30 | 45 | constructor(x, y, width, height, col) { |
31 | 46 | super(x, y, col); |
32 | | - this._width = width; |
33 | | - this._height = height; |
| 47 | + this.#width = width; |
| 48 | + this.#height = height; |
| 49 | + } |
| 50 | + |
| 51 | + get width() { |
| 52 | + return this.#width; |
| 53 | + } |
| 54 | + |
| 55 | + get height() { |
| 56 | + return this.#height; |
34 | 57 | } |
35 | 58 |
|
36 | 59 | get area() { |
37 | | - return this._width * this._height; |
| 60 | + return this.width * this.height; |
38 | 61 | } |
39 | 62 |
|
40 | 63 | draw(ctx) { |
41 | | - ctx.fillStyle = this._col; |
42 | | - ctx.fillRect(this._x, this._y, this._width, this._height); |
| 64 | + ctx.fillStyle = this.col; |
| 65 | + ctx.fillRect(this.x, this.y, this.width, this.height); |
43 | 66 | } |
44 | 67 | } |
45 | 68 |
|
46 | 69 | export class Circle extends Shape { |
| 70 | + #r; |
| 71 | + |
47 | 72 | constructor(x, y, r, col) { |
48 | 73 | super(x, y, col); |
49 | | - this._r = r; |
| 74 | + this.#r = r; |
| 75 | + } |
| 76 | + |
| 77 | + get r() { |
| 78 | + return this.#r; |
50 | 79 | } |
51 | 80 |
|
52 | 81 | get area() { |
53 | | - return Math.PI * this._r ** 2; |
| 82 | + return Math.PI * this.r ** 2; |
54 | 83 | } |
55 | 84 |
|
56 | 85 | draw(ctx) { |
57 | | - ctx.fillStyle = this._col; |
| 86 | + ctx.fillStyle = this.col; |
58 | 87 | ctx.beginPath(); |
59 | | - ctx.arc(this._x, this._y, this._r, 0, 2 * Math.PI); |
| 88 | + ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI); |
60 | 89 | ctx.fill(); |
61 | 90 | } |
62 | 91 | } |
0 commit comments