目次
4匹の仮想の猫を作って歩かせる
クラス
class Cat: """Cat class """ name = "cat" # メンバー変数 cWalk = 20 fp = "." def __init__(self, n ): # コンストラクタ self.name = n def Walk(self): # メソッド s = "" print("Walk" , "\t" ,end=("")) for i in range(self.cWalk): s += self.fp print( s ,"(" , self.name, ")" )
メインプログラム
if __name__ == "__main__": catList = ["pochi","tama","buchi","tora"] cats = dict() for c in catList: cats[c] = Cat( c ) cats["pochi"].cWalk = 3 cats["tama"].cWalk = 10 cats["buchi"].cWalk = 13 cats["tora"].cWalk = 4 for c in cats: cats[c].Walk() cats["tora"].footprint = "*" cats["tora"].Walk()