Sie sind auf Seite 1von 16

Thinking OOP

UVLE
UVLE Course: GmE205-GTH-19201
enrolment key: gme205-0sz0h2
What is Object Oriented Programming?
According Alan Kay (smalltalk and OOP):
Everything Is An Object.
Objects communicate by sending and receiving messages (in terms of objects).
Objects have their own memory (in terms of objects).
Every object is an instance of a class (which must be an object).
The class holds the shared behavior for its instances (in the form of objects in a
program list)
Being Object Oriented
MENTAL EXERCISE:
Recognizing Objects from Nouns
Imagine you want to measure the area and perimeter of squares, rectangles, circles, and ellipses.
Being Object Oriented
MENTAL EXERCISE:
Recognizing Objects from Nouns
Create function to calculate the areas.
Advantage: straightforward
Disadvantage: does not represent real world object

Create software objects that represent the state and behavior of a square, rectangle, circle, and an
ellipse
Being Object Oriented
MENTAL EXERCISE:
Recognizing Objects from Nouns
Data required for each shape:
Square Length of side
Rectangle Width and height
Circle Radius (usually labeled as r)
Ellipse Semi-major axis (usually labeled as a) and semi-minor axis (usually labeled as b)

Data required by each of the shapes is going to be encapsulated in each object


Data encapsulation is one of the major pillars of object-oriented programming
Being Object Oriented
MENTAL EXERCISE:
Generating blueprints for objects
Imagine that you want to draw and calculate the areas of four different rectangles.

Make a blueprint to simplify the process of drawing each rectangle with their different widths and
heights.
Being Object Oriented
MENTAL EXERCISE:
Generating blueprints for objects
A class is a blueprint or a template definition from which the objects are created.
Classes are models that define the state and behavior of an object.
Objects are also known as instances
Being Object Oriented
MENTAL EXERCISE:
Recognizing attributes/fields
With the Square class, it is necessary to know the length of side for each instance of this class, that
is, for each square object.
Thus, we need an encapsulated variable that allows each instance of this class to specify the value
of the length of side.

The Square class defines a floating point attribute named lengthOfSide


So we can say square1.lengthOfSide and it is different from square2.lengthOfSide
Being Object Oriented
MENTAL EXERCISE:
Recognizing attributes/fields
The variables defined in a class to encapsulate data for each instance of the class are known as
attributes or fields.
Each instance has its own independent value for the attributes or fields defined in the class.
Being Object Oriented
MENTAL EXERCISE:
Recognizing actions from verbs – methods
The functions or subroutines defined in a class to encapsulate the behavior for each instance of the
class are known as methods.
Each instance can access the set of methods exposed by the class.
The code specified in a method is able to work with the attributes specified in the class.
When we execute a method, it will use the attributes of the specific instance..
Being Object Oriented
MENTAL EXERCISE:
Recognizing actions from verbs – methods
CalculateArea: This returns a floating-point value with the calculated area
for the rectangle. The method returns the result of the multiplication of the
Width attribute value by the Height attribute value (Width * Height).
CalculatePerimeter: This returns a floating-point value with the
calculated perimeter for the rectangle. The method returns the sum
of two times the Width attribute value and two times the Height
attribute value (2 * Width + 2 * Height).
Being Object Oriented
MENTAL EXERCISE:
Recognizing actions from verbs – methods
Being Object Oriented
MENTAL EXERCISE:
Organizing the blueprints – classes
We can define the Shape class as an abstract class, because we don’t want to be able to create
instances of this class.
We want to be able to create instances of Square, Rectangle, Circle, or Ellipse.
In this case, the Shape abstract class declares two abstract methods.
We call CalculateArea and CalculatePerimeter abstract methods because the abstract class
declares them without an implementation, that is, without code.
The subclasses of Shape implement the methods because they provide code while maintaining the
same method declarations specified in the Shape superclass.
Abstraction and hierarchy are the two major pillars of object-oriented programming
Being Object Oriented
MENTAL EXERCISE:
Organizing the blueprints – classes
Reference
http://wiki.c2.com/?EarlyHistoryOfSmalltalk

Das könnte Ihnen auch gefallen