Created
September 5, 2026 08:24
-
-
Save thinkphp/cf3911f823005b37d79e73960fa4fa13 to your computer and use it in GitHub Desktop.
OOP.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| OOP | |
| - incapsulare | |
| Ascunderea datelor interne si controlul accesului la ele prin metode publice. | |
| Campurile: #id, #color, #x, #y, #borderWidth # visibile sunt declarate cu #, adica sunt private, | |
| nu pot fi accesate direct din afara clasei. Shape.#x ar da eroare | |
| Accesul la ele se face prin metode publice controlate | |
| getId() | |
| getColor() | |
| move(dx,dy) | |
| show() / hide() | |
| Datele sunt protejate | |
| - Mostenire | |
| - abstractizare | |
| - polimorfism | |
| */ | |
| //clasa de baza | |
| class Shape { | |
| #id; | |
| #color; | |
| #x; | |
| #y; | |
| #borderWidth; | |
| #visible; | |
| constructor(id, color, x, y, borderWidth = 1) { | |
| this.#id = id; | |
| this.#color = color; | |
| this.#x = x; | |
| this.#y = y; | |
| this.#borderWidth = borderWidth; | |
| this.#visible = true; | |
| } | |
| getId() { | |
| return this.#id | |
| } | |
| getColor() { | |
| return this.#color | |
| } | |
| getPosition() { | |
| return { | |
| x: this.#x, | |
| y: this.#y | |
| }; | |
| } | |
| move(dx, dy) { | |
| this.#x += dx | |
| this.#y += dy | |
| } | |
| hide() { | |
| this.#visible = false; | |
| } | |
| show() { | |
| this.#visible = true; | |
| } | |
| computeArea() { | |
| throw new Error("computeArea() trebuie implementata") | |
| } | |
| displayInfo() { | |
| console.log(`ID: ${this.#id} Color: ${this.#color} Position: ( ${this.#x}, ${this.#y} Border: ${this.#borderWidth}px Visible: ${this.#visible}`) | |
| } | |
| } | |
| //mostenire OOP | |
| //clasa derivata sau Subclasa | |
| class Circle extends Shape { | |
| #radius; | |
| constructor(id, color, x, y, borderWidth, radius) { | |
| super(id, color, x, y, borderWidth); | |
| this.#radius = radius; | |
| } | |
| computeArea() { | |
| return Math.PI * this.#radius ** 2; | |
| } | |
| } | |
| class Rectangle extends Shape { | |
| #width; | |
| #height; | |
| constructor(id, color, x, y, borderWidth, width, height) { | |
| super(id, color, x, y, borderWidth); | |
| this.#width = width | |
| this.#height = height | |
| } | |
| computeArea() { | |
| return this.#width * this.#height; | |
| } | |
| } | |
| class Triangle extends Shape { | |
| #base; | |
| #height; | |
| constructor(id, color, x, y, borderWidth, base, height) { | |
| super(id, color, x, y, borderWidth); | |
| this.#base = base; | |
| this.#height = height; | |
| } | |
| computeArea() { | |
| return (this.#base * this.#height) / 2; | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment