Tải bản đầy đủ (.ppt) (68 trang)

Overview of Object-Oriented Software Design and Java Programming potx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (280.76 KB, 68 trang )

1
Overview of
Object-Oriented Software
Design and Java Programming
Putting the Pieces Together!
2
Object-Oriented Design
A technique for developing a
program in which the solution
is expressed in terms of
objects self- contained
entities composed of data and
operations on that data.
3
Object Oriented Programming

Programmer thinks about and
defines the attributes and behavior of
objects.

Often the objects are modeled after
real-world entities.

Very different approach than
function-based programming (like C).
4
Reasons for OOP
Abstraction
Polymorphism
Inheritance
Encapsulation


Software Engineering Issues
5
Objects to Classes

A class defines the pattern used when
instantiating an object of that type.

A class generally contains private data and
public operations (called methods).
6
Class: Object Types

A Java class is an object type.

When you create the definition of a
class you are defining the attributes and
behavior of a new type.

Attributes are data members.

Behavior is defined by methods.
7
Creating an object

Defining a class does not result in
creation of an object.

Declaring a variable of a class type
creates an object. You can have many
variables of the same type (class).

Instantiation
8
Superclass and Subclass

Inheritance enables us to define a new class
(called a subclass) that inherits the properties of
an already existing class.

The newly derived class is then specialized by
adding properties specific to it.

The class being inherited from is the superclass.

The class that inherits properties is the subclass.
9
Defining Objects

An object-oriented program consists of
many objects.

An object is composed of identity, state
(attributes, data, and their current values)
and behavior (operations) .
10
Identity, State, Behavior

Identity is the property of an object that
distinguishes it from all other objects.

The failure to recognize the difference

between the name of the object and the
object itself is the source of many errors
in object-oriented (OO) programming.
11
Identity, S ta te, Behavior

The state of an object encompasses all of the
(static) properties of the object plus the current
(dynamic) values of each of these properties

A property is an inherent or distinctive
characteristic, trait, quality, or feature that
contribute to making an object uniquely that
object

We will use the word attribute, or data
member, to refer to the state of an object
12
Examples of State

Properties

Elevators travel up or down

Vending machines accept coins

Clocks indicate the current time

Values


Current floor

Number of coins deposited

The number of minutes since the last
hour
13
Identity, State, Behavior

Behavior is how an object acts and reacts,
in terms of state changes and interactions
with other objects.

An operation is some action that one
object performs upon another in order to
elicit a reaction.

We will use the word method to describe
object behavior in C++.

Invoking a method causes the behavior to
take place.
14
Classes

Classes are the definitions (or
blueprints) used to create objects. I’d
say: descriptions of objects.

To make a car the manufacturer must

first have a design from which to build
the first car. Then, once all the problems
are worked out, the design is used to
build all the cars of that model.
15
Objects

An object is an instance of a class.

If we have a class definition called Car,
then we can think of Audi, BMW, and
Corvette as each being an instance
(object) of the class Car, i.e., they are
each a type of car.
16
Object example
Audi 6 BMW Z3 Corvette

Notice that all objects are of the same type. All objects
are cars!
Car Car Car
17
Classes and Objects

An object is an instance of exactly one class

Corvette can not be an instance of a car class
and an instance of a plane class at the same
time.


An instance of a class, an object, belongs to
that particular class.

A Corvette is a car ⇒ Corvette belongs to the
class Car.
18
Classes

Once a class is defined you can create as
many instances of the class (objects from
the class) as you would like.

Once a blue print is completed for the 2004
Porsche 911, Porsche will use an assembly
line to build as many instances of the 2004
Porsche 911 as they wish.
19
Defining a class

Properties are variables which describe the
essential characteristics of an object.

Properties of a car: color, model, make, how many
doors, transmission type, direction of movement, etc.

Behaviors are methods that describe how the
object behaves and how the properties may be
modified.

Behavior of a car: braking, changing gears, opening

doors, moving forwards or backwards, etc.
20
Instance variables

The class definition will include parameter definitions
(properties) that represent data about a particular object,
instance variables.

Example, Joe's car may have 4 gallons of gas in it while
John's car has 10 gallons.

The amount of gas in each car may change without
affecting the amount of gas in the any other cars.

All instances (objects) of a class will have a set of instance
variables that are specific to that individual object.

The combination of the values of these instance variables
is known as the object’s state.
21
Instance variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
22
Class variables

The class definitions may also include

parameter definitions that represent data that
is shared by all class instances (objects),
called class variables.

In the case of the car class, we will define a
maximum allowed speed, by the law
(variable MaxSpeed). This will be the same
for each individual car.
23
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car
Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
MaxSpeed=155
24
Class variables

Class variables may also be used to
keep track of things such as how many
instances of a class exist.

Example: let’s create a counter the
records how many cars are in the
garage.
25
Class variables
Audi 6 BMW Z3 Corvette
Car Car Car

Car
MaxSpeed = 155 MaxSpeed = 165 MaxSpeed = 145
MaxSpeed
MaxSpeed=155
NumCars = 3

×