Title: Object-Oriented Programming
1Object-Oriented Programming
- Abstract Data Types (ADTs)
- Paradigm Evolution
- Characteristics of OOP
- Object-oriented Concept
- Imperative VS OOP
2Abstract Data Types (ADTs)
- ????????????????????????????????????????????
(user defined data type) - ??????????? structure ??????????? imperative
- ??????????????????????????????????????????
??????????? - ??????????????????????????????????????????????????
???????????????? - ??????????????????????????????????? ??????????
operations ??????????? - ???????????? ADT ????????????? 2 ????? ???
- Encapsulation ???????????????? operations
????????????????????????? - Information hiding ???????????????????????????????
??????? operations ?????????????????? ? - ???????????? ???? Modula-2, Ada --gt
object-based
3Paradigm Evolution
- Procedural 1950s -1970s (procedural
abstraction) - Data-Oriented early 1980s (data-oriented/ADTs)
- Object-Oriented late 1980s (Inheritance dynamic
binding)
Categories of OOP Language
- ???????? OOP ?????????????????????????????????????
???? ???? C, Ada95, CLOS, Scheme - ???????? OOP ??????????????????????????????????
imperative ???? ???? Eiffel, Java - ???? OOP ?????????? (Pure OOP) ???? ???? Smalltalk
4Characteristics of OOP
- Encapsulation
- Information hiding
- Inheritance
- Dynamic binding
Data
Operation
Data Operation
5OOP definitions
- Class
- Object, instance
- Subclass, derived class
- Superclass, parent class
- Method
- Message, behavior
- Inheritance
- Polymorphism
- Overriding
- Encapsulation
6Object-oriented concept
- ????? (Object) ????????????????? (state)
?????????????????????? ???????????????????????????
?? ???? ?????,?????????????,??????????????????????
?????? - ???????? --gt ?????
- ?????? ???? ??????? ??????????
- ??????? ???????????? ? ??????????????????,
?????????????????? - ???????????? Imperative ??????????????????????????
?????????????????????????
7Object-oriented concept
- Class ???????????????????????????????????????????
???? - Instance ?????????????????????????????????????????
? class ???? - ????????????????????????????????????????????
- ?? method ???????????????????? ?????????????
instance ???? - ??????????? instance ??? class ??????? instance
?????????????????????????????????
??????????????????????? - ?????????? instance ???? ?????????????? ?
????????????????????? - Class -gt Type
- Instance -gt Variable
8OO Thinking
9Procedural thinking
- Procedure draw(diagram d)
- begin
- for each shape s in diagram d do
- begin
- case s of
- box code to draw a box
- ellipse code to draw an ellipse
- ..
-
arc code to draw an arc
10Object-oriented concept
- Class ???????????????????????? ? ??? object
- ???????????????????????????? object
????????????????? instance ???? - Object ??????????????????????????????? message
- ????? object ?????? message ?? execute method
???????????? - Class ???????????????? 2 ?????? ???
- data member ??????????????????????
??????????????? ??????????????????????????
????????????????? object - method member ????????????????????????
?????????????? message ????????? ? ??? method
11Object-oriented concept
- ?????????????? class ??????? ????? class
??????????????????????????????????????????????????
????????????????????????????????? - class ???????????????????????????? ????????????
superclass ??? class ???? - class ?????????????????? class ???????????????????
??????????????????????????????????????????????????
???????? ?????????????? subclass ??? class ???? - ???????????? class ??????????????
?????????????????? ???? inheritance
12OO Thinking
Class Shape methods initialize, draw, offset,
setwidth, setheight, setalign variables
width, height, align
Class Box methods draw, offset
Class Ellipse methods draw, offset
Class Line methods initialize,draw variables
shadow
Class Text methods initialize,draw variables
string
Class Circle methods initialize
13Object-oriented concept
- Method overriding ??????????????????? method ??
subclass ?????????????????? method ?? parent
class ???????????????????????? method ?? parent
class ?????????????????? subclass - ??? binding ??????? method ??? instance ?????????
method ????? ??????? dynamic binding - ???????????????????? instance ??? class
?????????????? method ????????????????
polymorphism - polymorphism ?????????????????????????????????????
??????????????? ????????????????????????
14Object-oriented concept
- Method overloading ???????????? method ?????????
argument ???????????????????????????????????
??????????????? ? method ??????????????????????
???? method ????????????????????? 3 method ??????
argument ??????? ??? - function add(xinteger yinteger)
- function add(xreal yreal)
- function add(xinteger yreal)
- Multiple Inheritance ???????????? class ???????
class ??????????????? class
15Example in C
- class A
- public
- int x
- char f( )
- A( )
-
- class D public A
- int x
- int g( )
-
- class D ???? subclass ??? class A
- Object ??? class D ???????? 4 ??????
- x ???? data member ????????? (inherit) ?????
class A (Ax) - f ???? method ?????????????? class A
- x ???? data member ???????????????? class D
- g ???? method ???????????????? class D
Encapsulation Inheritance
16Example in C
- class Shape
- public Shape draw (Shape )
- return this
-
- class Ellipse public Shape
- public Shape draw (Shape )
- return this
-
- class Circle public Shape
- public Shape draw (Shape )
- return this
- Shape s
- s new Ellipse
- Shape p ...
- s-gtdraw(p) // Ellipsedraw(p)
- s new Circle
- s-gtdraw(p) // Circledraw(p)
Polymorphism Dynamic binding
17Example in C
- class List
- cell rear
- public List( )
- int empty( )
- protected
- void add(int)
- void push(int)
- int get( )
-
- class Queue public List
- public
- Queue( )
- int get( ) return Listget()
- void put(int x) add(x)
- ????????? object ?? class Queue
- Public function
- Queue added(constructor)
- get added
- put added
- Listempty inherited
- Protected function
- add inherited
- push inherited
- List get inherited
-
Information Hiding
18???????? OOP
- ?????????? ???????????????????????????????????????
????????? ?????????????????????????? object
????????????????????????????????????? - ?????????? ??????????????????????
- ???????????????? ?????????????? error ?????
- ?????????????????? (reusability)
?????????????????????????? - ?????????????????? ?????????? platform
19?????????? OOP
- ????????? ?????????????????????????????
???????????????????? ??? procedural - ??????????????????????????????
- ??????????????? ??????????? multiple inheritance
20Imperative VS Object-oriented
- Imperative
- Top-down design
- Procedure-oriented
- Algorithm
- Share global data
- Program-data separation
- Data transportation
- Single flow
- Object-oriented
- Bottom-up
- Object-oriented
- Behavior
- Information hiding
- Encapsulation
- Communication with messages
- Multiple objects
21Question
- ???????????????????
- ?????????????
- ???????????????????????????
- ???????????????????????????????????????????????
- ????? ??? ??????????????????????????
- ?????????????
- ????????????????????????