開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Learning Python (Mark Lutz (著)、Oreilly & Associates Inc)のPART Ⅵ.(Classes and OOP)、CHAPTER 29(Class Coding Details)、Test Your Knowledge: Quiz.1.~6.を解いてみる。
その他参考書籍
Test Your Knowledge: Quiz.1.~6.
コード(BBEdit)
sample.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- # abstract class class Abs: def __init__(self): raise Exception("Abs: abstract class") def method(self): raise NotImplementedError class Sup(Abs): # top level count = 0 def __init__(self, count=5): print('Hello, super class!') self.count = count Sup.count += 1 def method(self): print('Hello, super class method!') class Sup1(Abs): pass class Sub(Sup): def __init__(self, count=10): print('Sub class start') Sup.__init__(self, count) print('Sub class end') # completely replacing def method(self): # local scope x = 100 print('Hello, sub class method!') if __name__ == '__main__': try: a = Abs() except Exception as err: print(type(err), err, err.args) b = Sup() try: c = Sup1() except Exception as err: print(type(err), err, err.args) d = Sub() b.method() d.method()
入出力結果(Terminal)
$ ./sample.py <class 'Exception'> Abs: abstract class ('Abs: abstract class',) Hello, super class! <class 'Exception'> Abs: abstract class ('Abs: abstract class',) Sub class start Hello, super class! Sub class end Hello, super class method! Hello, sub class method! $
0 コメント:
コメントを投稿