Documente Academic
Documente Profesional
Documente Cultură
3-9
Abstraction
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 3
Abstraction
Abstraction
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 4
Abstraction
Abstraction Example
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 5
Abstraction
Abstraction Principle
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 6
Abstraction
Abstraction Pseudocode Example
• For example, when you add a Fly to the World it will also have
a maximum speed that it can move and an initial direction.
• Your code will add a Fly and specify the maximum speed it
can move and the starting direction.
• Here is the pseudo code:
– Create new Fly (4,90)
– Create new Fly (2,120)
– Create new Fly (3,270)
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 7
Abstraction
Abstraction Pseudocode Example
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 8
Abstraction
Abstraction Techniques
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 9
Abstraction
Abstraction Techniques Example
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 10
Abstraction
Constructor Using Variables
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 11
Abstraction
Constructor Using Variables
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 12
Abstraction
Programming to Place Instances
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 13
Abstraction
Constructor Example
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 14
Abstraction
Abstract Code to a Method
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 15
Abstraction
Abstract Code to a Method Example
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 16
Abstraction
Abstract Code to a Method Example
public BeeWorld()
{
super(600, 400, 1);
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 17
Abstraction
Simple Abstraction of Code
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 18
Abstraction
Create Baby Subclass
import greenfoot.*;
// An actor superclass that provides movement in four directions.
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 19
Abstraction
Create Movement Methods for Baby
Subclass
• Then, add the following movement methods:
– These methods simplify and abstract the Greenfoot API of
getX()/getY().
public void moveRight()
{
setLocation ( getX() + SPEED, getY() );
}
public void moveLeft()
{
setLocation ( getX() - SPEED, getY() );
}
public void moveUp()
{
setLocation ( getX(), getY() - SPEED);
}
public void moveDown()
{
setLocation ( getX(), getY() + SPEED);
}
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 20
Abstraction
Code the act() Method
• Code the act() method of the Baby Actor class so that its
instances move when the arrow keys are pressed.
• This abstraction hides and automates the more complex
code, only showing moveLeft(), moveRight(), etc.
public void act()
{
if (Greenfoot.isKeyDown("left") )
{
moveLeft();
}
if (Greenfoot.isKeyDown("right") )
{
moveRight();
}
}
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 21
Abstraction
Accessing Methods in Other Classes
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 22
Abstraction
Extending the BeeWorld Class
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 23
Abstraction
Accessing Other Objects Methods
• In the Bee class we could access the score method by using
the getWorld() method. You might try:
• Becomes…
BeeWorld myworld = (BeeWorld)getWorld();
myworld.updateScore();
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 25
Abstraction
Accessing Other Actors
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 26
Abstraction
Accessing Other Actors
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 27
Abstraction
Terminology
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 28
Abstraction
Summary
JF 3-9 Copyright © 2015, Oracle and/or its affiliates. All rights reserved. 29
Abstraction