開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc., Emacs(Text Editor)
- プログラミング言語: Python
『初めてのPython 第3版』(Mark Lutz 著、夏目 大 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-393-7)のIV部(クラスとオブジェクト指向プログラミング)、まとめ演習8.(動物の分類)を解いてみる。
その他参考書籍
まとめ演習8.(動物の分類)
sample.py
#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-
class Animal:
def say(self):
raise Exception(
"{0}: sayメソッドが定義されていない".format(self.__class__.__name__))
def reply(self):
self.say()
class Mammal(Animal): pass
class Cat(Mammal):
def say(self):
print("ニャー")
class Dog(Mammal):
def say(self):
print("ワン")
class Primate(Mammal):
def say(self):
print("Hello, world!")
class Hacker(Primate):
pass
if __name__ == '__main__':
spot = Cat()
spot.reply()
data = Hacker()
data.reply()
入出力結果(Terminal)
$ ./sample.py ニャー Hello, world! $
0 コメント:
コメントを投稿