if __name__ == "__main__":
#模組中可執行的程式碼例如下面的 cla22.py 檔案
class Demo:
__x = 0
def __init__(self, i):
self.__i = i
Demo.__x += 1
def __str__(self):
return str(self.__i)
def hello(self):
print("hello " + self.__str__())
@classmethod
def getX(cls):
return cls.__x
class Other:
def __init__(self, k):
self.k = k
def __str__(self):
return str(self.k)
def hello(self):
print("hello, world")
def bye(self):
print("Good-bye!", self.__str__())
class SubDemo(Demo, Other):
def __init__(self, i, j):
super().__init__(i)
self.__j = j
def __str__(self):
return super().__str__() + "+" + str(self.__j)
if __name__ == "__main__":
a = SubDemo(12, 34)
a.hello()
a.bye()
b = SubDemo(56, 78)
b.hello()
b.bye()
# 《程式語言教學誌》的範例程式
# http://pydoing.blogspot.com/
# 檔名:cla22.py
# 功能:示範 Python 程式
# 作者:張凱慶
# 時間:西元 2010 年 12 月第 39 行到第 45 行
if __name__ == "__main__":
a = SubDemo(12, 34)
a.hello()
a.bye()
b = SubDemo(56, 78)
b.hello()
b.bye()亦即判斷 __name__ 是否等於 "__main__" 的所在之處,若是相等,也就是 cla22.py 直接被執行, if 底下的程式碼就會被執行。
| 中英文術語對照 | |
|---|---|
| 模組 | module |
| 關鍵字 | keyword |
| 陳述 | statement |
| 屬性 | attribute |
參考資料
http://docs.python.org/py3k/tutorial/modules.html
http://docs.python.org/py3k/reference/simple_stmts.html
http://docs.python.org/py3k/tutorial/modules.html
http://docs.python.org/py3k/reference/simple_stmts.html
沒有留言:
張貼留言