razvandragos29 Posted July 27, 2014 Report Posted July 27, 2014 Salut invat in acest moment dupa Thinking in Java editia a 4-a si am intampinat o problema nu inteleg la ce se refera autorul prin "delegation" acesta este exemplu public class SpaceShipControls {void up(int velocity) {}void down(int velocity) {}void left(int velocity) {}void right(int velocity) {}void forward(int velocity) {}void back(int velocity) {}void turboBoost() {} public class SpaceShipDelegation {private String name;private SpaceShipControls controls =new SpaceShipControls();public SpaceShipDelegation(String name) {this.name = name;}// Delegated methods:public void back(int velocity) {controls.back(velocity);}public void down(int velocity) {controls.down(velocity);}public void forward(int velocity) {controls.forward(velocity);}public void left(int velocity) {controls.left(velocity);}public void right(int velocity) {controls.right(velocity);}public void turboBoost() {controls.turboBoost();}public void up(int velocity) {controls.up(velocity);}public static void main(String[] args) {SpaceShipDelegation protector =new SpaceShipDelegation("NSEA Protector");protector.forward(100);} Quote
M2G Posted July 27, 2014 Report Posted July 27, 2014 Chiar si in programare, multe cuvinte sunt imprumutate din alte domenii pentru a fi mai usor de abstractizat. Delegation - Wikipedia, the free encyclopediaClasa SpaceShipControls are definite anumite metode. SpaceShipDelegation implementeaza aceleasi metode (au acelasi nume) iar implementarea lor este sa apeleze metodele din SpaceShipControls.Deci daca ai un SpaceShipDelegation deleg = new SpaceShipDelegation("adsa"); si vrei sa apelezi metoda back.Se va apela defapt back din SpaceShipDelegation si pe urma back() din SpaceShipControls.Adica clasa SpaceShipDelegation delega apelul la SpaceShipControls.Cred ca urmeaza sa inveti despre interfete, clase abstracte si polimorfism si asta a fost introducerea. 1 Quote
razvandragos29 Posted July 27, 2014 Author Report Posted July 27, 2014 Mersi mult am inteles este un mod bun de a nu folosi mostenirea in anumite situatii. Quote