2. 2
OutlineOutline
1. 0. Object-oriented Programming
- What is OOP?- What is OOP?
1. 1.1. 1. Features of OOPFeatures of OOP
- Inheritance(- Inheritance(継承継承))
- Polymorphism(- Polymorphism(多態性多態性))
- Dynamic binding(- Dynamic binding(動的束縛動的束縛))
- Encapsulation(- Encapsulation(カプセル化カプセル化))
1. 2.1. 2. Exception handlingException handling
1. 3.1. 3. AppendixAppendix
3. 3
IntroductionIntroduction
Object-oriented Programming (OOP)Object-oriented Programming (OOP)::
a programming paradigm that representsa programming paradigm that represents
concepts as "concepts as "objectsobjects" that have data fields" that have data fields
((attributesattributes that describe the object) andthat describe the object) and
associated procedures known asassociated procedures known as methodsmethods..
(from Wikipedia “Object-oriented programming” (at 2013/05/11))(from Wikipedia “Object-oriented programming” (at 2013/05/11))
属性属性ととメソッドメソッドからなるからなるオブジェクトオブジェクトで概念を表現で概念を表現
オブジェクト間のオブジェクト間の相互作用相互作用としてプログラムを実装としてプログラムを実装
多くのプログラミング言語では、オブジェクトは多くのプログラミング言語では、オブジェクトはクラスクラスのの
インスタンスインスタンスとして実装として実装
message
message
object A:
m1
m2
a2
a1
object B:
m1
m2
a2
a1
4. 4
OOP vs Structured ProgrammingOOP vs Structured Programming
Structured Programming Language(like C, php, ...)Structured Programming Language(like C, php, ...)
OOP Language(like Java, Python, ...)OOP Language(like Java, Python, ...)
data1:
............
............
f1
data2:
............
............
f0
f2
int main()
O1
O0
O2
data is recorded as
attribute inside object
O3
5. 5
Class definition in PythonClass definition in Python
右のコードでは、クラス右のコードでは、クラス
dogdogを定義を定義
__init____init__は特殊なメソッドは特殊なメソッド
で、クラスのインスタンスで、クラスのインスタンス
が生成されるときに呼ばれが生成されるときに呼ばれ
るる((constructor))。。
PythonPythonでは、メソッドのでは、メソッドの
引数の最初は必ず引数の最初は必ずselfselfで、で、
selfselfはそのインスタンス自はそのインスタンス自
身身(momo(momoなどなど))を表す特殊を表す特殊
な引数。な引数。
ここでは、ここでは、momo, kuromomo, kuroはは
クラスクラスdogdogののinstance。
classclass dog(object):dog(object):
# constructor# constructor
defdef __init____init__(self, name):(self, name):
self.name = nameself.name = name
# deconstructor# deconstructor
defdef __del____del__(self):(self):
passpass
defdef getNamegetName(self):(self):
print “this is “ + self.nameprint “this is “ + self.name
>>> momo = dog(“Momo”)>>> momo = dog(“Momo”)
>>> momo.getName()>>> momo.getName()
this is Momothis is Momo
>>> kuro = dog(“Kuro”)>>> kuro = dog(“Kuro”)
>>> kuro.getName()>>> kuro.getName()
this is Kurothis is Kuro
6. 6
OutlineOutline
1. 0.1. 0. Object-oriented ProgrammingObject-oriented Programming
- What is OOP?- What is OOP?
1. 1. Features of OOP
- Inheritance(- Inheritance(継承継承))
- Polymorphism(- Polymorphism(多態性多態性))
- Dynamic binding(- Dynamic binding(動的束縛動的束縛))
- Encapsulation(- Encapsulation(カプセル化カプセル化))
1. 2.1. 2. Exception handlingException handling
1. 3.1. 3. AppendixAppendix
9. 9
Inheritance(Inheritance(継承継承))
「モモ」と「クロ」の子ど「モモ」と「クロ」の子ど
も「ポチ」は雑種も「ポチ」は雑種ZZ。。
多重継承を使えば、雑種も多重継承を使えば、雑種も
表現可能。表現可能。
classclass shiba(dog):shiba(dog):
defdef __init____init__(self, name):(self, name):
self.name = name + “(s)”self.name = name + “(s)”
classclass retriever(dog):retriever(dog):
passpass
classclass Z(shiba, retriever):Z(shiba, retriever):
passpass
>>> momo = shiba(“Momo”)>>> momo = shiba(“Momo”)
>>> momo.getName()>>> momo.getName()
this is Momo(s)this is Momo(s)
>>> kuro = retriever(“Kuro”)>>> kuro = retriever(“Kuro”)
>>> kuro.getName()>>> kuro.getName()
this is Kurothis is Kuro
>>> pochi = Z(“Pochi”)>>> pochi = Z(“Pochi”)
>>> pochi.getName()>>> pochi.getName()
this is Pochi(s)this is Pochi(s)
dog
shibainu retriever
object
momo kuroZ
pochi
15. 1
5
OutlineOutline
1. 0.1. 0. Object-oriented ProgrammingObject-oriented Programming
- What is OOP?- What is OOP?
1. 1.1. 1. Features of OOPFeatures of OOP
- Inheritance(- Inheritance(継承継承))
- Polymorphism(- Polymorphism(多態性多態性))
- Dynamic binding(- Dynamic binding(動的束縛動的束縛))
- Encapsulation(- Encapsulation(カプセル化カプセル化))
1. 2. Exception handling
1. 3.1. 3. AppendixAppendix
16. 1
6
Exception handling(Exception handling(例外処理例外処理))
Which do you like when you handling errors?Which do you like when you handling errors?
defdef divdiv(a, b):(a, b):
ifif b == 0:b == 0:
raiseraise ZeroDivisionErrorZeroDivisionError
elseelse::
returnreturn a / ba / b
defdef testtest(a, b):(a, b):
trytry::
print div(a, b)print div(a, b)
exceptexcept ZeroDivisionError:ZeroDivisionError:
print "divided by zero!"print "divided by zero!"
>>> test(4, 2)>>> test(4, 2)
22
>>> test(4, 0)>>> test(4, 0)
divided by zero!divided by zero!
defdef divdiv(a, b):(a, b):
ifif b == 0:b == 0:
returnreturn 00
elseelse::
returnreturn a / ba / b
defdef testtest(a, b):(a, b):
v = div(a, b)v = div(a, b)
ifif v == 0v == 0 andand a != 0:a != 0:
print “divided by zero!”print “divided by zero!”
elseelse::
print vprint v
>>> test(4, 2)>>> test(4, 2)
22
>>> test(4, 0)>>> test(4, 0)
divided by zero!divided by zero!
17. 1
7
Exception handling(Exception handling(例外処理例外処理))
エラー処理をどうするかエラー処理をどうするか??
想定されうるエラーに想定されうるエラーに
対して条件分岐で記述対して条件分岐で記述
特定の範囲で発生した特定の範囲で発生した
例外を捕捉を捕捉
Java: catch, throwJava: catch, throw
Python: except, raisePython: except, raise
エラー処理以外にも例外処エラー処理以外にも例外処
理を活用すると、理を活用すると、素敵な素敵なププ
ログラムを作成可能ログラムを作成可能((後述後述))
defdef testtest(a, b):(a, b):
trytry::
print a / bprint a / b
exceptexcept ZeroDivisionError:ZeroDivisionError:
print "divided by zero!"print "divided by zero!"
exceptexcept Exception:Exception:
print "embed exception"print "embed exception"
exceptexcept::
print "other exception"print "other exception"
elseelse::
print "no exception"print "no exception"
finallyfinally::
print "finally executed"print "finally executed"
>>> test(4, 2)>>> test(4, 2)
22
no exceptionno exception
finally executedfinally executed
>>> test(4, 0)>>> test(4, 0)
divided by zero!divided by zero!
finally executedfinally executed