#14章 对象
#14-1 创建一个简单的Ball类
class Ball: #告诉python我们建立一个类
def bounce(self):
if self.direction == "down":
self.direction = "up"
#14-2 使用Ball类
class Ball:
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball()
myBall.direction = "down"
myBall.color = "red"
myBall.size = "small"
print("I just created a ball.")
print("My ball is",myBall.size)
print("My ball is",myBall.color)
print("My ball's direction is",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is",myBall.direction)
#14-3 增加一个__init__的方法传递参数
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball ("red","small","down")
print("I just created a ball.")
print("My ball is",myBall.color) #书中代码是size,实际应该是color
print("My ball is",myBall.size)#书中代码是color,实际应该是size
print("My ball's direction is ",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is ",myBall.direction)
#14-4 使用__str__()改变打印对象的方式
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def __str__(self):
msg = ("Hi,I'm a " + self.size + " " + self.color + " ball!")
return msg
myBall = Ball("red","small","down")
print (myBall)
#14-5 热狗程序的开始部分
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
myDog = HotDog()
print(myDog.cooked_level)
print(myDog.cooked_string)
print(myDog.condiments)
print("Now I'm going o cook the hot dog")
myDog.cook(4)
print (myDog.cooked_level)
print (myDog.cooked_string)
#14-6 包含cook() add_condiments() __str__() HotDog
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def __str__(self):
msg = "hot dog"
if len(self.condiments) > 0:
msg = msg + "with"
for i in self.condiments:
msg = msg + i + ","
msg = msg.strip(",")
msg = self.cooked_string + "" +msg + "."
return msg
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
def addCondiment(self,condiment):
self.condiments.append(condiment)
myDog = HotDog()
print(myDog)
print("Cooking hot dog for 4 minutes...")
myDog.cook(4)
print(myDog)
print("Cooking hot dog for 3 more minutes...")
myDog.cook(3)
print(myDog)
print("What happens if I cook it for 10 more minutes?")
myDog.cook(10)
print(myDog)
print("Now , I'm going to add some stuff on my hot dog")
myDog.addCondiment("ketchup")
myDog.addCondiment("mustard")
print(myDog)
#14.8多态和继承
class Triangle:
def __init__(self,width,height):
self.width = width
self.height =height
def getArea(self):
area = self.width * self.height / 2.0
return area
class Square:
def __init__(self,size):
self.size = size
def getArea(self):
area = self.size * self.size
return area
myTriangle = Triangle(4,5)
mySquare = Square(7)
print(myTriangle.getArea())
print(mySquare.getArea())
class Game_object:
def __init__(self,name):
self.name = name
def pickUp(self):
pass
#put code here to add the object
#to the player's collection
class Coin(Game_object):
def __init__(self,value):
GameObject.__init__(self,"coin")
self.value = value
def spend(selt,buyer,seller):
pass
#14-1 创建一个简单的Ball类
class Ball: #告诉python我们建立一个类
def bounce(self):
if self.direction == "down":
self.direction = "up"
#14-2 使用Ball类
class Ball:
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball()
myBall.direction = "down"
myBall.color = "red"
myBall.size = "small"
print("I just created a ball.")
print("My ball is",myBall.size)
print("My ball is",myBall.color)
print("My ball's direction is",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is",myBall.direction)
#14-3 增加一个__init__的方法传递参数
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def bounce(self):
if self.direction == "down":
self.direction = "up"
myBall = Ball ("red","small","down")
print("I just created a ball.")
print("My ball is",myBall.color) #书中代码是size,实际应该是color
print("My ball is",myBall.size)#书中代码是color,实际应该是size
print("My ball's direction is ",myBall.direction)
print("Now I'm going to bounce the ball")
print()
myBall.bounce()
print("Now the ball's direction is ",myBall.direction)
#14-4 使用__str__()改变打印对象的方式
class Ball:
def __init__(self,color,size,direction):
self.color = color
self.size = size
self.direction = direction
def __str__(self):
msg = ("Hi,I'm a " + self.size + " " + self.color + " ball!")
return msg
myBall = Ball("red","small","down")
print (myBall)
#14-5 热狗程序的开始部分
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
myDog = HotDog()
print(myDog.cooked_level)
print(myDog.cooked_string)
print(myDog.condiments)
print("Now I'm going o cook the hot dog")
myDog.cook(4)
print (myDog.cooked_level)
print (myDog.cooked_string)
#14-6 包含cook() add_condiments() __str__() HotDog
class HotDog:
def __init__(self):
self.cooked_level = 0
self.cooked_string = "Raw"
self.condiments = []
def __str__(self):
msg = "hot dog"
if len(self.condiments) > 0:
msg = msg + "with"
for i in self.condiments:
msg = msg + i + ","
msg = msg.strip(",")
msg = self.cooked_string + "" +msg + "."
return msg
def cook(self,time):
self.cooked_level = self.cooked_level + time
if self.cooked_level >8:
self.cooked_string = "Charcoal"
elif self.cooked_level >5:
self.cooked_string = "Well-done"
elif self.cooked_level >3:
self.cooked_string = "Medium"
else:
self.cooked_string = "Raw"
def addCondiment(self,condiment):
self.condiments.append(condiment)
myDog = HotDog()
print(myDog)
print("Cooking hot dog for 4 minutes...")
myDog.cook(4)
print(myDog)
print("Cooking hot dog for 3 more minutes...")
myDog.cook(3)
print(myDog)
print("What happens if I cook it for 10 more minutes?")
myDog.cook(10)
print(myDog)
print("Now , I'm going to add some stuff on my hot dog")
myDog.addCondiment("ketchup")
myDog.addCondiment("mustard")
print(myDog)
#14.8多态和继承
class Triangle:
def __init__(self,width,height):
self.width = width
self.height =height
def getArea(self):
area = self.width * self.height / 2.0
return area
class Square:
def __init__(self,size):
self.size = size
def getArea(self):
area = self.size * self.size
return area
myTriangle = Triangle(4,5)
mySquare = Square(7)
print(myTriangle.getArea())
print(mySquare.getArea())
class Game_object:
def __init__(self,name):
self.name = name
def pickUp(self):
pass
#put code here to add the object
#to the player's collection
class Coin(Game_object):
def __init__(self,value):
GameObject.__init__(self,"coin")
self.value = value
def spend(selt,buyer,seller):
pass
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220205/viewspace-2076518/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/220205/viewspace-2076518/