Introduction to Object Oriented Programming
Hsuan-Tien Lin
Dept. of CSIE, NTU
OOP Class, March 1-2, 2010
要做一台投影機,需要哪些零件?
bulb fan case button
image processor lens
remote elec. board screw wire
要做一個CPU ,需要哪些零件?
IC board heat sink silicon wafer pin
transister logic gate capacitor led
要做一個POO BBS ,需要哪些零件?
machine space network 鄉民 , 鄉長 admin compiler login system manage system user system
board, gem, mail, edit, editor play, pet
vote, refer connection
模組化!
有好的模組才有好的系統
easy to debug make the goal clear divide and conquer easy to maintain/manage easy to modify/update easy to reuse
模組化:節省很多未來的時間和心力!
程 式輸入員v.s. 程式設計師
輸入員 : basic language skill
設計師 : good design skill + good language skill what’s the purpose of the program?
what’s the specialty of the language?
what’s the current need of the program?
what’s the future need of the program?
設計師:
願意用現在的專業付出,來節省未來的時間和心力!
不要只想寫出
用後即丟的程式
Noodle-Oriented Programming
whatever ingredients you put in, edible noodles are good noodles too salty? add more water
no vegetables? get whatever is in your refrigerator
spaghetti code: a program flow that looks like a bowl of spaghetti, i.e. twisted and tangled
NOP: generate whatever code that works for now
An Example of NOP
See OOPLotteryV0.java.
Applications of Noodle-Oriented Programming
不想讓人看懂 , 病毒 , etc.
交作業 , 趕時間 , etc.
一直WA 要修錯 , patch, 批改娘 , etc.
報復不喜歡的老師/助教 , 幫不喜歡的人寫程式
From NOP to Procedure Oriented Programming
organize the code
identify the purpose of procedures (what a block of code can do) isolate (modularize) the procedures (as individual functions) reuse the procedures (by function calls)
You basically learned those in the C class.
An Example of POP
See OOPLotteryV1.java.
Some Remaining Problems
the tokenizing program looks messy
1 p u b l i c s t a t i c S t r i n g [ ] getTokens ( S t r i n g s t r ) { 2 r e t u r n s t r . s p l i t ( " , " ) ;
3 }
–tell a str to splititself by ’,’
what if we want both department and names?
1 NameList [ i n d e x ∗ 3 + 2 ] , NameList [ i n d e x ∗ 3 + 0 ]
–can we usenames instead of calculating the offsets by ourselves?
Object Oriented Programming 101-1
group related data together in design
1 / ∗ C ∗ /
2 t y p e d e f s t r u c t{
3 char dept [ 1 0 0 ] ;
4 char ID [ 1 0 0 ] ;
5 char name [ 1 0 0 ] ;
6 } Record ;
1 / ∗ Java ∗ /
2 c l a s s Record {
3 S t r i n g dept ;
4 S t r i n g ID ;
S t r i n g name ;
Object Oriented Programming 101-2
use the struct/class to generate objects
1 / ∗ C ∗ /
2 Record r ;
3 Record∗ r p = ( Record ∗ ) m a l l o c (s i z e o f( Record ) ) ;
4 s t r c p y ( r . dept , " CSIE " ) ;
5 s t r c p y ( rp −>name , " HTLIN " ) ;
6 f r e e ( r p ) ;
1 / ∗ Java ∗ /
2 Record r = new Record ( ) ;
3 r . dept = " CSIE " ;
4 r . name = " HTLIN " ;
Object Oriented Programming 101-3
don’t “do something on” the object; let the object “do something”
1 / ∗ Java ∗ /
2 P r i n t S t r e a m ps = System . o u t ;
3 ps . p r i n t l n ( " CSIE " ) ;
4 S t r i n g s = " a , b , c " ;
5 t o k e n s = s . s p l i t ( " , " ) ;
From POP to Object Oriented Programming
use a procedure (strtok) to manipulate some representation of data (str)
1 / ∗ C ∗ /
2 char∗ s t r = " t o be s p l i t t e d . ’ ’ ;
3 char ∗ p ;
4 p = s t r t o k ( s t r , " " ) ;
5 w h i l e ( p ! = NULL ) {
6 p r i n t f ( "%s \ n " , p )
7 p = s t r t o k ( NULL , " " ) ;
8 }
ask an object (str) to perform an action (split)
1 / ∗ Java ∗ /
2 S t r i n g s t r = " t o be s p l i t t e d . ’ ’ ;
3 S t r i n g [ ] r e s = s t r . s p l i t ( " " ) ;
An Example of OOP
See OOPLotteryV2.java.
From Noodle to Procedural to Object
NOP: spaghetti code + (possibly spaghetti) data You can write NOP with virtually ANY languages Some easier to NOP (e.g. assembly), some harder POP: organized CODE + (possibly organized) data
using procedures as the basic module –maintain, reuse
action as separated procedures from data (do on the data)
C, Pascal
OOP: organized DATA + organized code (ACTION) using classes as the basic module
action are closely coupled with data
From Noodle to Procedural to Object
OOP: organized DATA + organized code (ACTION) using classes as the basic module
action are closely coupled with data (data do something)
Java, C++ (?), C#
You can write virtually any-OP with any language OO design: think in the OO way
OO language: help (force) you to think in the OO way
An OO Trip with Java String (see: Sec 1.3)
1 i m p o r t j a v a . l a n g . ∗ ;
2
3 p u b l i c c l a s s H e l l o W o r l d {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
5 S t r i n g s = " H e l l o World " ;
6 / / imagine : s ’ h o l d s ’ a l l c h a r a c t e r s i n s t e a d o f p o i n t i n g t o H
7 / / ( note : an i n a c c u r a t e d e s c r i p t i o n )
8
9 S t r i n g t = " H e l l o w o r l d " ;
10 System . o u t . p r i n t l n ( s . l e n g t h ( ) ) ;
11 / / a c t i o n f o r p r o b i n g o b j s t a t u s
12
13 System . o u t . p r i n t l n ( s . c h a r A t ( 3 ) ) ;
14 / / a c t i o n f o r p r o b i n g o b j s t a t u s
15 }
More OO Trip with Java String
1 i m p o r t j a v a . l a n g . ∗ ;
2
3 p u b l i c c l a s s H e l l o W o r l d {
4 p u b l i c s t a t i c v o i d main ( S t r i n g [ ] argv ) {
5 S t r i n g s = " H e l l o World " ;
6 S t r i n g t = " H e l l o w o r l d " ;
7
8 System . o u t . p r i n t l n ( s . equalsIgnoreCase ( t ) ) ;
9 / / a c t i o n c o n c e r n i n g one o b j and t h e o t h e r
10
11 System . o u t . p r i n t l n ( s . r e p l a c e ( ’ o ’ , ’ a ’ ) ) ;
12 / / a c t i o n f o r ’ m a n i p u l a t i n g ’ t h e o b j e c t
13 / / ( note : an i n a c c u r a t e d e s c r i p t i o n )
14
15 System . o u t . p r i n t l n ( s . c o n c a t ( t ) ) ;
16 System . o u t . p r i n t l n ( s . c o n c a t ( " l a l a " ) ) ;
Three Levels of OO
Object-Oriented Analysis (OOA):
what the system does
from (customer/system) needs to software models learn from your UML class
Object-Oriented Design (OOD):
how the system does it
from software model to class diagrams
(mostly) learn from your UML class; some in this class Object-Oriented Programming (OOP):
how to implement such a system
from class diagrams to class implementations learn from this class
Three Levels of OO
Object-Oriented Analysis (OOA):
what the system does
Object-Oriented Design (OOD):
how the system does it
Object-Oriented Programming (OOP):
how to implement such a system not necessarily separate levels
—connect the dots with the UML class by yourself