Last active
September 5, 2026 08:48
-
-
Save thinkphp/1e0a4472d794682f66e73db4b0fdc988 to your computer and use it in GitHub Desktop.
CLASS SHAPE
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 | |
| Expunere doar ceea ce este NECESAR , ascunzi datele de implementare | |
| metoda computeArea() din Shape este un exemplu de abstractizare | |
| clasa Shape defineste CE trebuie sa faca o forma (sa calculeze aria) , dar nu CUM - asta ramane la latitudinea fiecare clase derivate | |
| Este o abstractizare a conceptului de forma geometrica , fara a specifica detalii concrete de calcul | |
| De asemnea displayInfo abstractizeaza afisearea informatiilor - utilizatorul clasei apeleaza o singura metoda, fara sa stie ce campuri interne sunt afisare | |
| sau cum sunt formatate | |
| - 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; | |
| } | |
| //getters | |
| 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.getId()} 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; | |
| } | |
| getRadius() { | |
| return this.#radius; | |
| } | |
| } | |
| 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; | |
| } | |
| } | |
| //CREAREA OBIECTELOR | |
| const circle = new Circle( | |
| "C1", | |
| "red", | |
| 10, | |
| 20, | |
| 2, | |
| 5 | |
| ); | |
| const rectangle = new Rectangle( | |
| "R1", | |
| "blue", | |
| 30, | |
| 50, | |
| 1, | |
| 10, | |
| 5 | |
| ) | |
| const triangle = new Triangle( | |
| "T1", | |
| "green", | |
| 50, | |
| 60, | |
| 3, | |
| 8, | |
| 5 | |
| ) | |
| //Polimorfismul in action | |
| const shapes = [ circle, rectangle, triangle ] | |
| for(const shape of shapes) { | |
| console.log(`${shape.getId()} AREA = ${shape.computeArea()}`) //apel polimorfic | |
| shape.displayInfo() //apel polimorfic | |
| } | |
| /* | |
| Desi shapes contine obiecte de diferite tipuri (Circle , Rectangle, Triangle) codul apeleaza ACEEASI METODA | |
| computeArea() de fiecare data, dar fiecare obiect executa PROPRIA IMPLEMENTARE | |
| Circle.computeArea() =====>>>> pi * r * r | |
| Rectangle.computeArea() =====>> width * height | |
| Triangle.computeArea() =====> (base * height) / 2 | |
| Acesta este polimorfis; interfata comuna cu acelasi nume, comportament diferit in functie de tipul real al obiectul la | |
| RUNTIME | |
| incapsulare: #private campurile plus metode getter, move, hide, show | |
| Abstractizare computeArea() definit ca abstract in Shape | |
| Mostenire Circle, REctangle si Triangle extends Shape | |
| Polimorfism : Bucla for care apeleaza computeArea polimorfic | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment