Sie sind auf Seite 1von 65

Chapter 1

Encapsulation

2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Overview
1.1
Software development process
Encapsulation
1.2
Terminology and concepts related to objects
1.3
Expands on chapter subjects
Software Development
Essential that programs be correct.
Contains no bugs
Efficient
No more time or memory than necessary
General-purpose
Ability to build upon program for future use.
Rapidly developed
Software Development
Tradeoffs in a computer program
Software Development
In some applications lives may literally depend
on correctness of software.
To ensure correctness, thoroughly test.
Practical concerns often lead to the release of
buggy software.
Software Development
Data structure
A way of organizing information
Algorithm
Step-by-step process for doing something
Software Development
To make the best choices, we would like to
know as much as possible about how our
program will be used.
What kinds of Data
Which operations will be most common
Software Development
If a program is to be used very heavily be sure
to optimize.
We can save development time by reusing code.
Try to create general-purpose components
Example: write a method which can sort an array of any
length, containing any values of any comparable type
(numbers, letters, strings, and so on).
This general-purpose code tends to be less efficient than code
written for a specific use.
Also once it is written and thoroughly documented we never need
to think about its inner workings again.
Java has huge, general purpose libraries.
Software Development
Most development time is spent debugging.
Reduce debugging time by investing time in design
and testing.
Hastily thrown together programs can become
difficult to maintain.
Software Development
Encapsulation
The division of a program into distinct components
which have limited interaction.
Polymorphism
The use of the same word or symbol to mean
different things in different contexts.
Inheritance
The ability to specify that a program is similar to
another program, delineating only the differences.
Software Development
Encapsulation makes it easier to rapidly
develop correct programs because a
programmer only hast to consider a few things
when writing any one components of the
program.
Information hiding
The workings of a component should not be visible
from the outside.
Software Development
Software engineering
The study of how to develop correct, efficient,
general-purpose programs in a reasonable amount
of time.
Software Development
Software development cycle
Software Development
Design
Deciding what the program is going to look like.
Problem specification
The task of stating precisely what a program is
supposed to do.
Breaking the program down into components
Software Development
Implementation
Writing code
Move from a description of a program to a working
program.
Software Development
Testing
Run the program
Verify that it does what it is supposed to do.
Software Development
Maintenance
Changes to make, new features to add, bugs to fix.
More iterations of the software development cycle
Software Development
Top-down approach
The entire program should be designed in exquisite
detail, then implemented, then tested.
Making all decisions up front, we avoid wasting time
implementing unnecessary components.
Bottom-up approach
Design some simple component, implement it, test
it, expand the design very slightly.
Software Development
Most software development falls between these
two extremes.
Encapsulation allows us to break up the
software development cycle.
Software Development
Software Development
The ability to concentrate on a single
component makes it much easier to rapidly
develop correct, efficient, general-purpose
code.
Integrate the components in a high-level
implementation phase and then test the entire
system.
Classes and Objects
Classes
Predominantly a description of a set of similar
objects
Encapsulated component of the program
We can create multiple instances of a class.
Each instance is a different object.
Classes and Objects
Create two instances of the class of beetles
Create one instance of the class of dice
Classes and Objects
In Java a class begins with an upper-case letter
A file must have the same name as the class
and a .java extension.
Classes and Objects
Classes and Objects
An object has two kinds of components:
Fields
Represent its current state
Field values vary from one instance to another.
Methods
Actions it can perform
We ask objects to do things to themselves
Example: we don't roll a die, we ask it to roll itself.
Classes and Objects
Classes and Objects
Static
Not associated with an instance of a class.
Instance field or instance variable
A different value for each instance of a class.
Private
They cannot be accessed by methods in other
classes.
Example of information hiding
Classes and Objects
By making such small, incremental changes to
the code, we can avoid spending a lot of time
hunting for bugs.
Constructor
Method that initializes all of the fields of an object.
Same name as the class.
In a nonstatic method, the current object can
refer to itself with the keyword this.
Classes and Objects
Classes and Objects
Classes and Objects
Classes and Objects
Default value
Java will initialize a field in an object.
Numbers such as int and double the value is 0
Booleans the default is false.
Chars the default is unprintable character with ASCII and
0 for Unicode.
Arrays the default is null.
Classes and Objects
Other classes should be able to get at the fields
of an object only through methods.
Accessor or getter
Returns the value of some field
Mutator or setter
Changes the value of some field within the object.
Classes and Objects
Classes and Objects
Classes and Objects
Nonstatic fields or nonstatic methods are
associated with instances.
We can use this only within nonstatic methods.
Nonstatic methods are sometimes called instance
methods.
A static method is associated with the entire
class rather than with individual instances.
Static methods are sometimes called class
methods.
Classes and Objects
Classes and Objects
Classes and Objects
When we are done with a class, we generate
automatic documentation with javadoc
Private fields are not shown because that
information is not required outside the scope of
the class.
Example of encapsulation.
http://java.sun.com has similar documentation
for all of Java's hundreds of built-in classes.
Known as the application programming interface
or API
Using Objects
Using Objects
Using Objects
Using Objects
The toString() Method
When a nontrivial piece of code is needed more
than once, move it off into a separate encapsulated
component.
Example: String representations are common used to
output object states.
toString() returns a String representation of the
current state of the Beetle.
Using Objects
Since toString() is so common passing just the
object to println() implies the use of the
toString() method.
Example: System.out.println(bug);
Possible Beetle representations
Using Objects
Using Objects
Using Objects
Using Objects
Dic Class toString():
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Using Objects
Summary
Three Principles
Encapsulation
Polymorphism
Inheritance
Encapsulation
Division of a program into distinct components
(such as methods and classes) which have limited
interaction.
Summary
Software development cycle has three phases
Design
Implementation
Testing
Top-down approach
Bottom-up approach
Encapsulation is enforced by information hiding.
Private fields of an object can be accessed only
within the object's class.

Das könnte Ihnen auch gefallen