我鼓励他们开始学习OO
class Location: name="a place" description = "A dark place. there are exits to the North and East" exits = "North","East" def __str__(self): return "%sn%s"%(self.name,self.description)class Player: current_location = "Home" inventory = ["Blue Key","Magic Thumbtacks"] health = 100 name = "Unknown" def __init__(self,name): self.name = nameplayer = Player("Player 1")loc = Location()print locx = input("Input:")
老实说,游戏是一个困难的概念(甚至是文字冒险)。但是我将直接从面向对象的概念开始,从长远来看,它们将从中受益更多。
当然,这个例子很小,并且省略了很多实现细节。
一个不相关但更好的面向对象示例是:
class Animal: voice = '...' def speak(self): return "A %s Says '%s'"%(self.__class__.__name__, self.voice)class Dog(Animal): voice = "Bark, Bark"class Duck(Animal): voice = "Quack, Quack"print Dog().speak()print Duck().speak()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)