Академический Документы
Профессиональный Документы
Культура Документы
Chapter 8
Object-Based Programming
(Not Responding)
• That message means that some Windows object
is not responding to messages.
• Some program called a method, but Windows
failed to respond. (No surprise)
Class
MyClass boop;
At this point, the reference called
“boop” does not actually point to any
existing object.
Soon, it will point to an object of type
MyClass, but now the object doesn’t exist.
What?
How?
Who?
Java I—Copyright © 2000 Tom
OOP Vocabulary
state behavior identity
• Key characteristics:
(What) What is the object’s state?
(How) What is the object’s behavior?
(Who) What is the object’s identity?
’598-22-7893’,’$10.00’,’22.25’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee marie;
marie = new HourlyEmployee(‘Mary’,’J.’,
’555-24-1516’,’$30.00’,’0’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee marie;
marie = new HourlyEmployee(‘Mary’,’J.’,
’555-24-1516’,’$30.00’,’0’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
HourlyEmployee theodore;
theodore = new HourlyEmployee(‘Ted’,’L.’,
’681-22-9875’,’$10.00’,’22’);
HourlyEmployee david;
david = new HourlyEmployee(‘Dave’,’D.’,
’198-99-0098’,’$15.00’,’8’);
marie.setSSN( ‘444-33-1264’ );
ClassName.primitive_variable
ClassName.Object_variable.
use
containment ( “has-a” )
inheritance ( “is-a” )
Use
Containment ( “has-a” )
inheritance ( “is-a” )
Account
Order message
• A method of Order :
creates
receives or
returns
objects of class Account .
• Design Tip:
Avoid the “use” relationship whenever
you can. If you “use” somebody else’s class,
then any changes to that class can break your
class.
Account
Order message
Order
Account
Java I—Copyright © 2000 Tom
OOP Vocabulary
Relationships Between Classes: containment
use
“has-a”
“is-a”
Java I—Copyright © 2000 Tom
Techniques
for
Using Objects
JLabel howdy;
howdy = new JLabel( “How Are Ya?” );
hello = howdy;
• Now, both references point to the exact same object.
howdy = hello;
out1 = howdy.getText();
out2 = hello.getText();
out1 = howdy.getText();
out2 = hello.getText();
H O W E V E R...
javac -d Time2.java
public Time1()
{
setTime( 0, 0, 0 );
}
public Time1()
{
setTime( 0, 0, 0 );
} In keeping with encapsulation, the member-
access modifiers declare our instance variables
public void setTime( int h, int m, int s )
{ private.
hour = ( ( h >= 0 && h < 24 ) ? h : 0 );
minute = ( ( m >= 0 && m < 60 ) ? m : 0 );
When this
second = (class gets
( s >= instantiated,
0 && s < 60 ) ? s the
: 0 only
way
);
}
to access these variables is through the methods
of the class.
Java I—Copyright © 2000 Tom
import java.text.DecimalFormat;
public Time1()
{
setTime( 0, 0, 0 );
}
( More Later )
Java I—Copyright © 2000 Tom
public String toUniversalString()
{ Method toString() originates in class Object. When
you want to see
DecimalFormat what is in
twoDigits = the
new instance variables"00"
DecimalFormat( of the
);
class, you call the toString() method. Every class
return twoDigits.format( hour ) + ":" +
you create should override this
twoDigits.format( method
minute and +create
) + ":" its own
twoDigits.format(copy.
second );
}
System.exit( 0 );
}
} Java I—Copyright © 2000 Tom
import javax.swing.JOptionPane;
SavingsAccount.interestRate
SavingsAccount.getInterestRate()
SavingsAccount.getInterestRate()
ClassName.staticMethod( parameters );
variables and
methods