Lecture 23: Inheritance CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Menu • Objects Review • Inheritance • PS6 17 March 2004 CS 200 Spring 2003

Download Report

Transcript Lecture 23: Inheritance CS200: Computer Science University of Virginia Computer Science David Evans http://www.cs.virginia.edu/evans Menu • Objects Review • Inheritance • PS6 17 March 2004 CS 200 Spring 2003

Lecture 23:
Inheritance
CS200: Computer Science
University of Virginia
Computer Science
David Evans
http://www.cs.virginia.edu/evans
Menu
• Objects Review
• Inheritance
• PS6
17 March 2004
CS 200 Spring 2003
2
Objects
• When we package state and
procedures together we have
an object
• Programming with objects is
object-oriented programming
17 March 2004
CS 200 Spring 2003
3
make-number
(define make-number
(lambda (n)
(lambda (message)
(cond
((eq? message 'value)
(lambda (self) n))
Why don’t we just use n?
((eq? message 'add)
(Well see why later today.)
(lambda (self other)
(+ (ask self 'value)
(ask other 'value))))))))
17 March 2004
CS 200 Spring 2003
4
ask
Lecture 22:
(define (ask object message)
(object message))
(define (ask object message . args)
(apply (object message) object args))
17 March 2004
CS 200 Spring 2003
5
(define make-number
(lambda (n)
(lambda (message)
(cond
((eq? message 'value)
(lambda (self) n))
((eq? message 'add)
(lambda (self other)
(+ (ask self 'value)
(ask other 'value))))))))
> (define san
(make-number 3))
> (ask san 'value)
3
> (ask san 'add
(make-number 4))
7
17 March 2004
global
environment
+ : #<primitive:+>
make-number:
san:
n:
3
parameters:
body: ((lambda …
parameters: message
body: (cond ((eq? …
CS 200 Spring 2003
6
There are many kinds of numbers…
•
•
•
•
Whole Numbers (0, 1, 2, …)
Integers (-23, 73, 0, …)
Fractions (1/2, 7/8, …)
Floating Point (2.3, 0.0004, 3.14159)
• But they can’t all do the same things
– We can get the denominator of a fraction, but
not of an integer
17 March 2004
CS 200 Spring 2003
7
make-fraction
(define make-fraction
(lambda (numerator denominator)
(lambda (message)
(cond
((eq? message 'value)
(lambda (self) (/ numerator denominator))
((eq? message 'add)
Same as in
(lambda (self other)
make-number
(+ (ask self 'value) (ask other 'value)))
((eq? message ‘get-numerator)
Note: our add
(lambda (self) numerator))
method evaluates
to a number, not
((eq? message ‘get-denominator)
a fraction object
(lambda (self) denominator))
(which would be
)))))
better).
17 March 2004
CS 200 Spring 2003
8
Why is redefining add a bad thing?
• Cut-and-paste is easy but…
• There could be lots of number methods
(subtract, multiply, print, etc.)
• Making the code bigger makes it harder to
understand
• If we fix a problem in the number add
method, we have to remember to fix the
copy in make-fraction also (and real,
complex, float, etc.)
17 March 2004
CS 200 Spring 2003
9
Inheritance
17 March 2004
CS 200 Spring 2003
10
make-fraction
(define (make-fraction numer denom)
(let ((super (make-number #f)))
(lambda (message)
(cond
((eq? message 'value)
(lambda (self) (/ numer denom)))
((eq? message 'get-denominator)
(lambda (self) denom))
((eq? message 'get-numerator)
(lambda (self) numer))
(else
(super message))))))
17 March 2004
CS 200 Spring 2003
11
Using Fractions
> (define half (make-fraction 1 2))
> (ask half 'value)
1/2
> (ask half 'get-denominator)
2
> (ask half 'add (make-number 1))
3/2
> (ask half 'add half)
1
17 March 2004
CS 200 Spring 2003
12
> (trace ask)
> (trace eq?)
> (ask half 'add half)
|(ask #<procedure> add #<procedure>)
|
|
|
|
|
|
|
|
|
|
(eq?
#f
(eq?
#f
(eq?
#f
(eq?
#f
(eq?
#t
add value)
add get-denominator)
add get-numerator)
add value)
add add)
17 March 2004
| (ask #<procedure> value)
| |(eq? value value)
| |#t
| 1/2
| (ask #<procedure> value)
| |(eq? value value)
| |#t
| 1/2
|1
1
CS 200 Spring 2003
13
make-number
make-fraction
> (trace ask)
> (trace eq?)
> (ask half 'add half)
|(ask #<procedure> add #<procedure>)
|
|
|
|
|
|
|
|
|
|
(eq?
#f
(eq?
#f
(eq?
#f
(eq?
#f
(eq?
#t
add value)
add get-denominator)
add get-numerator)
add value)
add add)
17 March 2004
| (ask #<procedure> value)
| |(eq? value value)
| |#t
| 1/2
| (ask #<procedure> value)
| |(eq? value value)
| |#t
| 1/2
|1
1
CS 200 Spring 2003
14
Inheritance
Inheritance is using the definition of one
class to make another class
make-fraction uses make-number to
inherit the behaviors of number
17 March 2004
CS 200 Spring 2003
15
• English
A Fraction is a kind of Number.
Number
• C++
Fraction is a derived class whose base
class is Number
• Java
Fraction
Fraction extends Number.
• Eiffel
Fraction inherits from Number.
Note: people
sometimes draw
this different ways
• Beta
Fraction is a subpattern of Number.
• Smalltalk (72)
Didn’t have inheritance!
17 March 2004
CS 200 Spring 2003
16
CS 200:
Number
Fraction inherits from Number.
Fraction is a subclass of Number.
Fraction
The superclass of Fraction is
Number.
17 March 2004
CS 200 Spring 2003
17
Inheritance and Subtyping
• Inheritance: reusing the definition of one
class to make a new kind of class
make-fraction uses make-number
“fraction inherits from number”
• Often confused with subtyping which is
saying one kind of object can be used
where another kind of object is expected
17 March 2004
CS 200 Spring 2003
18
Subtyping
• Subtyping is very important in statically
typed languages (like C, C++, C#, Java,
Pascal) where you have to explicitly
declare a type for all variables:
method Number add (Number n) { … }
Because of subtyping, either a Number or a Fraction
(subtype of Number) could be passed as the argument
• CS200 won’t cover subtyping (although we
will talk more about types later)
17 March 2004
CS 200 Spring 2003
19
PS6
Make an adventure game
programming with objects
Many objects in our game have
similar properties and behaviors,
so we use inheritance.
17 March 2004
CS 200 Spring 2003
20
object
PS6 Classes
physical-object
mobile-object
thing
student inherits from person
which inherits from mobile-object
which inherits from physical-object
which inherits from object.
17 March 2004
person
student
CS 200 Spring 2003
place
make-class is the
procedure for
constructing
objects in the
class class
police-officer
21
object
PS6 Objects
physical-object
mobile-object
thing
person
student
17 March 2004
CS 200 Spring 2003
Alyssa P. Hacker
place
Cabal
Hall
Recursa
(make-place name)
evaluates to an object
that is an instance of
the class place.
police-officer
22
Are there class hierarchies
like this in the real world or
just in fictional worlds like
Charlottansville?
17 March 2004
CS 200 Spring 2003
23
Microsoft Foundation Classes
CButton inherits from CWnd inherits from CObject
“A button is a kind of window is a kind of object”
17 March 2004
CS 200 Spring 2003
24
Node
PathInterpolator
Interpolator
Behavior
RotationPathInterpolator
Leaf
Object
SceneGraphObject
Not at all uncommon to have
class hierarchies like this!
Java
Class
Hierarchy Diagram
173D
March
2004
CS 200 Spring 2003
http://java.sun.com/products/java-media/3D/collateral/j3dclass.html
25
Charge
• PS6
– Programming with Objects
– Due Date change:
• Previously Monday, now due Friday 26
March
• Or, turn in Monday without doing
questions 7 and 8
• Friday: Is there anything that cannot be
computed?
17 March 2004
CS 200 Spring 2003
26