Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Paradigmas de Linguagens de Programacao - Aula #7

1,337 views

Published on

Paradigmas de Linguagens de Programacao
Aula #7
Prof. Ismar Frango

Published in: Education
  • Be the first to comment

Paradigmas de Linguagens de Programacao - Aula #7

  1. 1. Paradigmas de Linguagens de Programação Paradigma Orientado a Objetos Aula #8 (CopyLeft)2009 - Ismar Frango ismar@mackenzie.br
  2. 2. “ There are only two kinds of languages: the ones people complain about and the ones nobody uses.” Bjarne Stroustrup Binding em LOO
  3. 5. 22/09/09
  4. 7. invariância covariância contravariância   
  5. 10. 22/09/09 ?
  6. 11. ?
  7. 12. Asteroid SpaceShip Thing Dispatching em LOO
  8. 13. abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Thing other) { if (other instanceof Asteroid ) { // handle Asteroid-Asteroid collision } else if (other instanceof Spaceship ) { // handle Asteroid-Spaceship collision } } } class Spaceship extends Thing { public void collideWith ( Thing other) { if (other instanceof Asteroid ) { // handle Spaceship-Asteroid collision } else if (other instanceof Spaceship ) { // handle Spaceship-Spaceship collision } } }
  9. 14. abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Asteroid other) { System.out.println("handle Asteroid-Asteroid collision"); } public void collideWith ( Spaceship s) { System.out.println("handle Asteroid-Spaceship collision"); } } class Spaceship extends Thing { public void collideWith ( Asteroid a) { System.out.println("handle Spaceship-Asteroid collision"); } public void collideWith ( Spaceship other) { System.out.println("handle Spaceship-Spaceship collision"); } } 
  10. 15. abstract class Thing { public abstract void collideWith ( Thing other); } class Asteroid extends Thing { public void collideWith ( Thing other) {} public void collideWith ( Asteroid other) { System.out.println("handle Asteroid-Asteroid collision"); } public void collideWith ( Spaceship s) { System.out.println("handle Asteroid-Spaceship collision"); } } class Spaceship extends Thing { public void collideWith ( Thing other) {} public void collideWith ( Asteroid a) { System.out.println("handle Spaceship-Asteroid collision"); } public void collideWith ( Spaceship other) { System.out.println("handle Spaceship-Spaceship collision"); } } 
  11. 16. Asteroid ExplodingAsteroid SpaceShip GiantSpaceShip Thing
  12. 17. class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } };
  13. 18. class SpaceShip {}; class GiantSpaceShip : public SpaceShip {}; class Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;Asteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;Asteroid hit a GiantSpaceShip&quot; << endl; } }; class ExplodingAsteroid : public Asteroid { public : virtual void CollideWith (SpaceShip&) { cout << &quot;ExplodingAsteroid hit a SpaceShip&quot; << endl; } virtual void CollideWith (GiantSpaceShip&) { cout << &quot;ExplodingAsteroid hit a GiantSpaceShip&quot; << endl; } }; Asteroid theAsteroid; SpaceShip theSpaceShip; GiantSpaceShip theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShip); theAsteroid . CollideWith (theGiantSpaceShip); Asteroid * theAsteroidReference = new ExplodingAsteroid (); theAsteroidReference -> CollideWith (theSpaceShip); theAsteroidReference -> CollideWith (theGiantSpaceShip); SpaceShip & theSpaceShipReference = theGiantSpaceShip; theAsteroid . CollideWith (theSpaceShipReference); theAsteroidReference -> CollideWith (theSpaceShipReference);
  14. 19. class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip {}; Asteroid hit a SpaceShip ExplodingAsteroid hit a SpaceShip SpaceShip & theSpaceShipReference = theGiantSpaceShip; Asteroid & theAsteroidReference = theExplodingAsteroid; theSpaceShipReference. CollideWith (theAsteroid); theSpaceShipReference. CollideWith (theAsteroidReference); class SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; class GiantSpaceShip : public SpaceShip { virtual void CollideWith ( Asteroid & inAsteroid) { inAsteroid. CollideWith (* this ); } }; Asteroid hit a GiantSpaceShip ExplodingAsteroid hit a GiantSpaceShip
  15. 20. O Padrão Visitor

×