Upload
tran-cao-cuong-tran
View
233
Download
0
Embed Size (px)
Citation preview
8/11/2019 Oop Handout Princple Programming Language
1/26
8/11/2019 Oop Handout Princple Programming Language
2/26
Outline
1 OOP Introduction
2 Scala
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 2 / 2 9
8/11/2019 Oop Handout Princple Programming Language
3/26
Object-Oriented Programming Goals
Programs as collections of collaborating objects
Object has public interface, hidden implementation
Objects are classfied 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 / 2 9
I d i T OOP L
8/11/2019 Oop Handout Princple Programming Language
4/26
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 / 2 9
S I t t F t f OOP
8/11/2019 Oop Handout Princple Programming Language
5/26
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 / 2 9
D t Ab t ti
8/11/2019 Oop Handout Princple Programming Language
6/26
Data Abstraction
An abstract data type is data type that satisfies thefollowing two conditions:
Encapsulation:
Attributes and operations are combined in a single
syntactic unitcompiled separately
Information Hiding:
The access to members are controlled
Reliability increased
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 7 / 2 9
Information Hiding
8/11/2019 Oop Handout Princple Programming Language
7/26
Information Hiding
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 8 / 2 9
ADT Example
8/11/2019 Oop Handout Princple Programming Language
8/26
ADT Example
Operations on a stack:
create(stack)
destroy(stack)
empty(stack)
push(stack,element)pop(stack)
top(stack)
Client code:. . .c r e a t e ( s t k 1 ) ;push ( s t k1 , c o l o r 1 ) ;push ( s t k1 , c o l o r 2 ) ;
i f ( ! empty ( s t k1 ) )temp = t o p ( s t k 1 ) ;
. . .
Implementation can be adjacent or linked list. Client: dontcare!
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 9 / 2 9
Class Hierarchy
8/11/2019 Oop Handout Princple Programming Language
9/26
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
8/11/2019 Oop Handout Princple Programming Language
10/26
Inheritance
A subclass is able to inheritnon-privatemembers ofits superclasses
A subclass can add its own members andoverrideits 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
8/11/2019 Oop Handout Princple Programming Language
11/26
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 solveddifferently 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
8/11/2019 Oop Handout Princple Programming Language
12/26
Class vs. Object; Method vs. Message
Class vs. Object
A class defines the abstract characteristics of a thing
its attributes or properties and
its behaviors or methods or features.
A Dog hasfurand 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 barkA 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
8/11/2019 Oop Handout Princple Programming Language
13/26
Polymorphism
Polymorphism: different objects can respond to the
same message in different ways.
Dynamic binding
Pointx,ydraw()move(newX,newY)
Circle radius
draw()
p.draw()
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 15/29
Other Concepts
8/11/2019 Oop Handout Princple Programming Language
14/26
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
8/11/2019 Oop Handout Princple Programming Language
15/26
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
8/11/2019 Oop Handout Princple Programming Language
16/26
Classes and Objects in Scala
Class
class [1]
abstract class [5]
trait [2]
case class [3]
Object
new
object
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 19/29
Example [7]
8/11/2019 Oop Handout Princple Programming Language
17/26
Example [7]
class R a t io n a l ( n : I n t , d : I n t ) {r e q u ir e ( d ! = 0)
p r iv a te v al g = gcd ( n . abs , d . abs )p r iv a t e def gcd ( a : I n t , b : I n t ) : I n t =
i f ( b == 0 ) a else gcd ( b , a % b )va l numer = n / gva l denom = d / g
def t h is( n : I n t ) = t h i s( n , 1 )
def + ( t h a t : R at io na l ) : R at io na l =new R a t i o n a l (
numer t h a t . denom + t h a t . numer denom,denom that .denom
)
o v e rr i d e d ef t o S t r i n g = numer + " / " + denom}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 20/29
Example on Abstract class [4]
8/11/2019 Oop Handout Princple Programming Language
18/26
Example on Abstract class [4]
a b s tr a c t c la ss El em ent {
def c o n t en t s : A r ra y [ S t r i n g ] / / no body : abstractva l h e i g h t = c o n t en t s . l e n g t hva l w i dt h =
i f ( h e ig h t == 0) 0 else c o n t en t s ( 0 ) . l e n g t h}class A r ra y El e me n t ( c o n t s : A r r a y [ S t r i n g ] )
extends El em ent {def c o n t en t s : A r ra y [ S t r i n g ] = c o nt s
}class L in eE le men t ( s : S t r i n g )
extends A r ra y El e me n t ( A r r a y ( s ) ) {
o v e rr i d e d ef w id th = s . l e n gt ho v e rr i d e d ef h e i g ht = 1
}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 21/29
Example on Object [1]
8/11/2019 Oop Handout Princple Programming Language
19/26
a p e o Object [ ]
object Element {
def elem ( c o n t e n t s : A r r a y [ S t r i n g ] ) : El em en t =new ArrayEl em ent ( con t ent s )
def elem ( l i n e : S t r i n g ) : Element =
new L i ne E le me n t ( l i n e )}
va l space = Element . elem ( " " )va l h e l l o = E le me nt . elem ( A r r a y ( " h e l l o " , " w o r l d " ) )
Which kind of Element will be assigned tospaceand
hello?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 22/29
Example on Case Class [3]
8/11/2019 Oop Handout Princple Programming Language
20/26
p [ ]
a b s tr a c t c la ss Expr
case class Var ( name : S t r i n g ) extends Exprcase class Number (num : Double ) extends Exprcase class UnOp ( o p e r a t o r : S t r i n g , a rg : Ex pr )
extends Expr
case class BinOp ( o p e r a t o r : S t r i n g ,l e f t : Expr , r i g h t : Expr ) extends Expr
va l v = Var ( " x " )va l op = BinOp ( " + " , Number ( 1 ) , v )
v . nameop . l e f t
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 23/29
Example 1 on Traits [9]
8/11/2019 Oop Handout Princple Programming Language
21/26
p [ ]
Reusability:
a b s tr a c t c la ss B i r d
t r a i t F l y i n g {def f ly Me ss : S t r i n gdef f l y ( ) = p r i n t l n ( f l yM es s )
}t r a i t Swimming {
def swim ( ) = p r i n t l n ( " I m swimming " )}class Penguin extends B i r d with Swimming
class Hawk extends B i r d with Swimming with F l y in g {
va l f ly Me ss = " I m a good f l y e r "}class F r i g a t e b i r d extends B i r d with F ly i n g {
va l f l yM es s = " I m an e x c e l l e n t f l y e r "}
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 24/29
Example 2 on Traits [2]
8/11/2019 Oop Handout Princple Programming Language
22/26
p [ ]
Stackable Modifications:
a b s tr a c t c la ss I nt Qu eu e {
def g et ( ) : I n tdef pu t ( x : I n t ) }
class BasicIntQueue extends I nt Qu eu e {p r iv a te v al 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 pu t ( x : I n t ) { b uf += x } }t r a i t Doubl ing extends I nt Qu eu e {a b st r a c t o v er r id e d ef p ut ( 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 I nt Qu eu e {a b st r a c t o v er r id e d ef p ut ( x : I n t ) { super . p u t ( x + 1 ) } }
va l queue =new BasicIntQueue with I n c r e m e n t i n g with Doubl ing
queue . put ( 10 )queue . g e t ( ) / / ? ? ?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 25/29
Scala Hierarchy [8]
8/11/2019 Oop Handout Princple Programming Language
23/26
y [ ]
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 26/29
Access Modifiers [6] in Scala
8/11/2019 Oop Handout Princple Programming Language
24/26
publicprotected
private
protected[]
private[]
Example,
package Assignment. . .
protected[ Assignme nt ] v al f i e l d 1 = 100p r i v a t e[ t h i s ] va l f i e l d 2 = 2 ;
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 27/29
Summary
8/11/2019 Oop Handout Princple Programming Language
25/26
What are still in your mind?
PhD. Nguyen Hua Phung Object-Oriented Programming in Scala 28/29
References
8/11/2019 Oop Handout Princple Programming Language
26/26
[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
http://www.artima.com/pins1ed/classes-and-objects.htmlhttp://www.artima.com/pins1ed/classes-and-objects.htmlhttp://www.artima.com/pins1ed/traits.htmlhttp://www.artima.com/pins1ed/traits.htmlhttp://www.artima.com/pins1ed/case-classes-and-pattern-matching.htmlhttp://www.artima.com/pins1ed/case-classes-and-pattern-matching.htmlhttp://www.artima.com/pins1ed/abstract-members.htmlhttp://www.artima.com/pins1ed/abstract-members.htmlhttp://www.artima.com/pins1ed/composition-and-inheritance.htmlhttp://www.artima.com/pins1ed/composition-and-inheritance.htmlhttp://www.artima.com/pins1ed/packages-and-imports.htmlhttp://www.artima.com/pins1ed/packages-and-imports.htmlhttp://www.artima.com/pins1ed/functional-objects.htmlhttp://www.artima.com/pins1ed/functional-objects.htmlhttp://www.artima.com/pins1ed/scalas-hierarchy.htmlhttp://www.artima.com/pins1ed/scalas-hierarchy.htmlhttp://joelabrahamsson.com/learning-scala-part-seven-traits/http://joelabrahamsson.com/learning-scala-part-seven-traits/http://joelabrahamsson.com/learning-scala-part-seven-traits/http://www.artima.com/pins1ed/scalas-hierarchy.htmlhttp://www.artima.com/pins1ed/functional-objects.htmlhttp://www.artima.com/pins1ed/packages-and-imports.htmlhttp://www.artima.com/pins1ed/composition-and-inheritance.htmlhttp://www.artima.com/pins1ed/abstract-members.htmlhttp://www.artima.com/pins1ed/case-classes-and-pattern-matching.htmlhttp://www.artima.com/pins1ed/traits.htmlhttp://www.artima.com/pins1ed/classes-and-objects.html