Professional Documents
Culture Documents
08, 2016
1 OOP Introduction
Animal
radius
Circle draw()
B C
radius
Circle draw()
p.draw()
Dynamic binding
Class
class [1]
abstract class [5]
trait [2]
case class [3]
Object
new <class name>
<case class name>
object
class R a t i o n a l ( n : I n t , d : I n t ) {
r e q u i r e ( d != 0)
def t h i s ( n : I n t ) = t h i s ( n , 1 )
def + ( t h a t : R a t i o n a l ) : R a t i o n a l =
new R a t i o n a l (
numer ∗ t h a t . denom + t h a t . numer ∗ denom ,
denom ∗ t h a t . denom
)
a b s t r a c t class Element {
def c o n t e n t s : A r r a y [ S t r i n g ] / / no body : a b s t r a c t
val height = contents . length
val width =
i f ( h e i g h t == 0 ) 0 else c o n t e n t s ( 0 ) . l e n g t h
}
class ArrayElement ( c o n t s : A r r a y [ S t r i n g ] )
extends Element {
def c o n t e n t s : A r r a y [ S t r i n g ] = c o n t s
}
class LineElement ( s : S t r i n g )
extends ArrayElement ( A r r a y ( s ) ) {
override def w i d t h = s . l e n g t h
override def h e i g h t = 1
}
object Element {
a b s t r a c t class Expr
t r a i t Flying {
def f l y M e s s : S t r i n g
def f l y ( ) = p r i n t l n ( f l y M e s s )
}
t r a i t Swimming {
def swim ( ) = p r i n t l n ( " I ’m swimming " )
}
Stackable Modifications:
a b s t r a c t class IntQueue {
def g e t ( ) : I n t
def p u t ( x : I n t ) : U n i t }
class BasicIntQueue extends IntQueue {
p r i v a t e v a l b u f = new A r r a y B u f f e r [ I n t ]
def g e t ( ) = b u f . remove ( 0 )
def p u t ( x : I n t ) : U n i t = { b u f += x } }
t r a i t Doubling extends IntQueue {
a b s t r a c t override def p u t ( x : I n t ) { super . p u t ( 2 ∗ x ) } }
t r a i t I n c r e m e n t i n g extends IntQueue {
a b s t r a c t override def p u t ( x : I n t ) { super . p u t ( x + 1 ) } }
v a l queue =
new BasicIntQueue with I n c r e m e n t i n g with Doubling
queue . p u t ( 1 0 )
queue . g e t ( ) / / ? ? ?
trait A {
d e f name : S t r i n g
}
t r a i t B extends A {
o v e r r i d e d e f name : S t r i n g =" c l a s s b "
}
t r a i t C extends A {
o v e r r i d e d e f name : S t r i n g =" c l a s s c "
}
c l a s s D extends B w i t h C { }
v a r c l a s s _ d = new D
p r i n t l n ( c l a s s _ d . name )
B: B->A->AnyRef->Any
C: C->A->AnyRef->Any
C->B->A->AnyRef->Any
D: D->C->B->A->AnyRef->Any
Dr. Nguyen Hua Phung Object-Oriented Programming 27 / 46
Scala Hierarchy [8]
protected
private
protected[<name>]
private[<name>]
Example,
package Assignment
...
protected [ Assignment ] v a l f i e l d 1 = 100
private [ t h i s ] val f i e l d 2 = 2;
class Employee :
@classmethod =>to define class method
def c r e a t e ( c l s , n , s ) : =>first parameter for class
p r i n t ( c l s . empCount ) =>access to static fields
return c l s ( n , s )
@staticmethod =>to define static method
def i s H i g h S a l ( s ) : =>no parameter for class
i f s > 8: =>unable to access any fields
p r i n t ( " High S a l a r y " )
o b j = Employee . c r e a t e ( "Nam" , 3 0 ) =>Employee is
passed to cls
Employee . i s H i g h S a l ( 3 0 ) =>Employee is used to re-
solve
B C
D
Method Resolution Order: determine search
sequence of a class
B(A) C(A)
D(B,C)
L(A) = [A,object]
L(B) = [B] + merge(L(A),[A]) = [B,A,object]
L(C) = [C] + merge(L(A),[A]) = [C,A,object]
L(D) = [D]+merge(L(B),L(C),[B,C]) = [D,B,C,A,object]
merge([B,A,o],[C,A,o],[B,C]) =>
B is a good head => [B]+merge([A,o],[C,A,o],[C])
A is NOT a good head, check C, and C is a good
head => [B,C] + merge([A,o],[A,o],[])
A is a good head => [B,C,A] + merge([o],[o],[])
o is a good head => [B,C,A,o]
Dr. Nguyen Hua Phung Object-Oriented Programming 39 / 46
super, isinstance, type
Overloading:
def f u n c ( param1 , param2 = 0 ) : pass
func ( 1 , 2 )
f u n c ( " asbc " )
Universal Polymorphism:
Parametric Polymorphism Subtyping Polymorphism
class A : class A :
def func1 ( s e l f ) : def func1 ( s e l f ) :
p r i n t ( "A" ) p r i n t ( "A" )
def func2(self): pass def func2(self): pass
class B : class B ( A ) :
def func1 ( s e l f ) : def func1 ( s e l f ) :
p r i n t ( "B" ) p r i n t ( "B" )
for x in [A ( ) , B ( ) ] : for x in [A ( ) , B ( ) ] :
x . func1 ( ) x . func1 ( )
x.func2() x.func2()
Dr. Nguyen Hua Phung Object-Oriented Programming 41 / 46
Some other issues
is vs. ==
is: True just when they are same object
==: True even when they are different objects but their
attributes are equal
x,y = [1,2,3],[1,2,3]
x is y => False
id(x) == id(y) => False
x == y => True