Sie sind auf Seite 1von 26

Object-Oriented Programming in

Scala
PhD. Nguyen Hua Phung
HCMC University of Technology, Viet Nam
06, 2014
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 1 / 29
Outline
1
OOP Introduction
2
Scala
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 2 / 29
Object-Oriented Programming Goals
Programs as collections of collaborating objects
Object has public interface, hidden implementation
Objects are classed according to their behavior
Objects may represent real-world entities or entities
that produce services for a program
Objects may be reusable across programs
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 4 / 29
Introduction To OOP Languages
There are many object-oriented programming (OOP)
languages
Some are pure OOP language (e.g., Smalltalk).
Newer languages do not support other paradigms but
use their imperative structures (e.g., Java and C).
Some support procedural and data-oriented
programming (e.g., Ada and C++).
Some support functional program (e.g., Scala)
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 5 / 29
Some Important Features of OOP
Abstract data types
Encapsulation
Information Hiding
Class Hierarchy
Inheritance
Polymorphism - Dynamic Binding
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 6 / 29
Data Abstraction
An abstract data type is data type that satises the
following two conditions:
Encapsulation:
Attributes and operations are combined in a single
syntactic unit
compiled separately
Information Hiding:
The access to members are controlled
Reliability increased
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 7 / 29
Information Hiding
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 8 / 29
ADT Example
Operations on a stack:
create(stack)
destroy(stack)
empty(stack)
push(stack,element)
pop(stack)
top(stack)
Client code:
. . .
cr eat e ( st k1 ) ;
push ( st k1 , col or 1 ) ;
push ( st k1 , col or 2 ) ;
i f ( ! empty ( st k1 ) )
temp = t op ( st k1 ) ;
. . .
Implementation can be adjacent or linked list. Client: dont
care!
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 9 / 29
Class Hierarchy
A class may have some Subclasses
A class may have one/many Superclass(es)
Animal
Fish
Whale
Mammal
Dog Cat
WingedAnimal
Bat
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 10 / 29
Inheritance
A subclass is able to inherit non-private members of
its superclasses
A subclass can add its own members and override
its inherited members
Single vs. multiple inheritance
Inheritance increases the reusability in OOP
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 11 / 29
Diamond Problem in Multiple Inheritance
A
B
D
C
if D inherits from B and C different versions of the
same behaviour, which version will be effective in D?
this problem, called diamond problem, is solved
differently in different OO language
is there the diamond problem in Java?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 13 / 29
Class vs. Object; Method vs. Message
Class vs. Object
A class denes the abstract characteristics of a thing
its attributes or properties and
its behaviors or methods or features.
A Dog has fur and is able to bark
An object is a particular instance of a class.
Lassie is a dog
Method vs. Message
A method describes a behavior of an object.
A dog can bark
A message is a process at which a method of an
object is invoked.
Lassie barks
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 14 / 29
Polymorphism
Polymorphism: different objects can respond to the
same message in different ways.
Dynamic binding
Point
x,y
draw()
move(newX,newY)
Circle
radius
draw()
p.draw()
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 15 / 29
Other Concepts
There are two kinds of variables in a class:
Class variables
Instance variables
There are two kinds of methods in a class:
Class methods accept messages to the class
Instance methods accept messages to objects
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 16 / 29
Scala
Invented by Martin Ordesky at EPFL, Lausanne,
Switzerland.
Similar to Java
Work smoothly with Java
Run on Java Virtual Machine
OOP + FP
Include lexer and parser generator
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 18 / 29
Classes and Objects in Scala
Class
class [1]
abstract class [5]
trait [2]
case class [3]
Object
new <class name>
<case class name>
object
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 19 / 29
Example [7]
class Rat i onal ( n : I nt , d : I n t ) {
r equi r e ( d ! = 0)
pri vat e val g = gcd ( n . abs , d . abs )
pri vat e def gcd ( a : I nt , b : I n t ) : I n t =
i f ( b == 0) a el se gcd ( b , a % b)
val numer = n / g
val denom = d / g
def t hi s ( n : I n t ) = t hi s ( n , 1)
def + ( t hat : Rat i onal ) : Rat i onal =
new Rat i onal (
numer t hat . denom + t hat . numer denom,
denom t hat . denom
)
overri de def t oSt r i ng = numer +" / " + denom
}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 20 / 29
Example on Abstract class [4]
abst ract class El ement {
def cont ent s : Ar r ay [ St r i ng ] / / no body : abst ract
val hei ght = cont ent s . l engt h
val wi dt h =
i f ( hei ght == 0) 0 el se cont ent s ( 0 ) . l engt h
}
class ArrayEl ement ( cont s : Ar r ay [ St r i ng ] )
extends Element {
def cont ent s : Ar r ay [ St r i ng ] = cont s
}
class Li neEl ement ( s : St r i ng )
extends ArrayEl ement ( Ar r ay ( s ) ) {
overri de def wi dt h = s . l engt h
overri de def hei ght = 1
}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 21 / 29
Example on Object [1]
obj ect El ement {
def elem( cont ent s : Ar r ay [ St r i ng ] ) : Element =
new ArrayEl ement ( cont ent s )
def elem( l i n e : St r i ng ) : Element =
new Li neEl ement ( l i n e )
}
val space = Element . elem( " " )
val hel l o = El ement . elem( Ar r ay ( " hel l o " , " wor l d " ) )
Which kind of Element will be assigned to space and
hello?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 22 / 29
Example on Case Class [3]
abst ract class Expr
case class Var ( name: St r i ng ) extends Expr
case class Number (num: Double ) extends Expr
case class UnOp( oper at or : St r i ng , arg : Expr )
extends Expr
case class BinOp ( oper at or : St r i ng ,
l e f t : Expr , r i g h t : Expr ) extends Expr
val v = Var ( " x " )
val op = BinOp ( " +" , Number ( 1) , v )
v . name
op . l e f t
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 23 / 29
Example 1 on Traits [9]
Reusability:
abst ract class Bi r d
t r a i t Fl yi ng {
def f l yMess : St r i ng
def f l y ( ) = p r i n t l n ( f l yMess )
}
t r a i t Swimming {
def swim( ) = p r i n t l n ( " I m swimming " )
}
class Penguin extends Bi r d with Swimming
class Hawk extends Bi r d with Swimming with Fl yi ng {
val f l yMess = " I m a good f l y e r "
}
class Fr i gat ebi r d extends Bi r d with Fl yi ng {
val f l yMess = " I m an excel l ent f l y e r "
}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 24 / 29
Example 2 on Traits [2]
Stackable Modications:
abst ract class IntQueue {
def get ( ) : I n t
def put ( x : I n t ) }
class Basi cI nt Queue extends IntQueue {
pri vat e val buf = new Ar r ayBuf f er [ I n t ]
def get ( ) = buf . remove ( 0)
def put ( x : I n t ) { buf += x } }
t r a i t Doubl i ng extends IntQueue {
abst ract overri de def put ( x : I n t ) { super . put ( 2 x ) } }
t r a i t I ncr ement i ng extends IntQueue {
abst ract overri de def put ( x : I n t ) { super . put ( x +1) } }
val queue =
new Basi cI nt Queue with I ncr ement i ng with Doubl i ng
queue . put ( 10)
queue . get ( ) / / ???
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 25 / 29
Scala Hierarchy [8]
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 26 / 29
Access Modiers [6] in Scala
public
protected
private
protected[<name>]
private[<name>]
Example,
package Assi gnment
. . .
protected [ Assi gnment ] val f i e l d 1 = 100
pri vat e [ t h i s ] val f i e l d 2 = 2;
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 27 / 29
Summary
What are still in your mind?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 28 / 29
References
[1] Classes and Objects, http://www.artima.com/pins1ed/classes-and-objects.html, 19 06 2014.
[2] Traits, http://www.artima.com/pins1ed/traits.html, 19 06 2014.
[3] Case Class and Pattern Matching,
http://www.artima.com/pins1ed/case-classes-and-pattern-matching.html,19 06 2014.
[4] Abstract Members, http://www.artima.com/pins1ed/abstract-members.html, 19 06 2014.
[5] Compositions and Inheritance,
http://www.artima.com/pins1ed/composition-and-inheritance.html, 19 06 2014.
[6] Packages and Imports, http://www.artima.com/pins1ed/packages-and-imports.html, 19 06
2014.
[7] Functional Objects, http://www.artima.com/pins1ed/functional-objects.html, 19 06 2014.
[8] Scala Hierarchy, http://www.artima.com/pins1ed/scalas-hierarchy.html, 19 06 2014.
[9] Learning Scala part seven -Traits,
http://joelabrahamsson.com/learning-scala-part-seven-traits/, 19 06 2014.
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 29 / 29

Das könnte Ihnen auch gefallen