Вы находитесь на странице: 1из 21

Object Oriented Concepts

Basic OO Terms
Objects
Classes
Class attributes
Relationships
Methods (Services)
Encapsulation
Polymorhpism
Inheritance
Reuse
Objects and Classes
Object
An abstract or concrete thing, an entity
Each object instance must have a unique ID
Attribute
A property of an object
Classes
A groups of similar objects
Have the same kind of attributes
Example Class
The Class of Automobile Objects
Attritubtes
Vehicle Identification Number
Manufacturer
Color
Type (sedan, roadster, pickup, etc)
Price
Etc
Example Object Instances

VIN 8183764 VIN 132417


Manu Ford Manu Chevy
Color Fuscia Color Taupe
Type Pickup Type Sedan
Price $28,000 Price $15,000
Relationships
Association (Connection) between classes
1:1, 1:M, M:N
Whole Part (Composition)
The object is part of another object
A cylinder head is part of a motor.
Buttons, text boxes, etc. are part of a window.
Have cardinality and optionality.
Class Methods or Services
Methods
Procedures
What an object knows how to do.
Similar to functions in other languages.
Used to model object behavior.
Associated with classes.
Thus, classes have both attributes and
methods.
Automobile Class
Attritubtes Methods (Services)
VIN Insert
Manufacturer Update

Color Delete

Type
Price
Encapsulation
Attributes and methods are packaged
together.
Information hiding
Internal aspects are concealed or hidden.
Objects are prevented from changing other
objects.
Message Passing
User and other objects send messages to
objects.
Objects can be told to perform a service
(method).
Can be told to change itself.
May result from
Clicking a button,
Choosing from a list, etc.
Calling a method
Polymorhpism
Methods with the same name may do
different things.
Depends on the object class they are
associated with.
Accounting example:
Real Property and Computer Equipment are two
asset classes.
Both have Methods called Calculate Depreciation.
Real Property is depreciated on a straight-line basis
Computer equipment uses sum of the years digits.
Hierarchies and Inheritance
Classes arranged in hierarchies of sub-
classes and super-classes
Attributes and methods are inherited by
sub-classes
Inherited properties may be over-ridden.
Automobile Class Hierarchy
Motor Vehicles
# of Wheels
Engine

Automobiles Over- Motorcycles


4 Wheels ridden 2 Wheels
# of Doors Sidecar

Pickups Local attributes


Bed Length
Accounting Class Hierarchy
Accounts
Number
Name
Balance

Assets Liabilities
Depreciation Method

Buildings Computers
Depreciation Depreciation
(Straight line) (Accelerated)
Insurance Policy Object
Attributes:
Policy Number
Policy Type
Owner
Premium
Method:
Calculate Penalty
Attribute Inheritance
Insurance Policies

Home Date built Date built defined for all


home policies.

Flood Plain
Earthquake
Date built
Feet from source Inherited Date built
Surveyor Building material
Last quake date

These attributes are local


Method Inheritance
Insurance Policies Calculate age

These mehtods
are inherited
Home Calculate age

Flood Plain
Earthquake
Calculate age
Calculate penalty Calculate age
Calculate penalty

These are local


Local Behaviors
Insurance Policies

Home Local behaviors may have


different characteristics.

Flood Plain
Earthquake
Calculate penalty
(100/Feet to source) Calculate penalty
* Appraised Value * .1 $0.50 * (Days
since last quake)
Polymorphism
Insurance Policies

Home
Calculate penalty message

Flood Plain
Earthquake
Calculate penalty
(100/Feet to source) Calculate penalty
* Appraised Value * .1 $0.50 * (Days
since last quake)
Benefits
Encapsulation provides data independence.
e.g., change an from integer to decimal type
without affecting other objects.
Inheritance provides a parsimonious environment.
Polymorphism allows applications to be generic,
removing the need for handling "special cases"
with Case statements of Ifs.
Objects can be placed in a library for reuse.
Summary
Objects entities
Classes groups of similar objects
Class attributes properties
Relationships associations, part-whole
Methods (Services) procedures
Encapsulation data hiding
Polymorhpism same method name,different proc.
Inheritance properties passed to children
Reuse object libraries

Вам также может понравиться