14.1 真实世界中的对象
#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)
print('--------------------------------------------')
print (myBall)#你会得到这样一个奇怪的东西
“魔法”方法 : __str__()
#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 to 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章对象动手试一试
#14.1对应银行账户的类如下所示:
class BankAccout:
def __init__(self,acct_number,acct_name):
self.acct_number = acct_number
self.acct_name = acct_name
self.balance = 0.0
def displayBalance(self):
print("The account balance is:",self.balance)
def deposit(self,amount):
self.balance = self.balance + amount
print("You deposited",amount)
print("The new balance is:",self.balance)
def withdraw(self,amount):
if self.balance >=amount:
self.balance = self.balance - amount
print ("You withdrew",amount)
print ("The new balance is :",self.balance)
else:
print("You tried to withdraw",amount)
print("The account balance is:",self.balance)
print("Withdrawal denied. Not enough funds.")
myAccount = BankAccout(234567,"Warren Sande")
print("Account name:",myAccount.acct_name)
print("Account number:",myAccount.acct_number)
myAccount.displayBalance()
myAccount.deposit(34.52)
myAccount.withdraw(12.25)
myAccount.withdraw(30.8)
#14.2建立一个账户信息,计算利息
##class InterestAccount(BankAccount):
## def __init__(self,acct_number,acct_name,rate):
## BankAccount.__init__(self,acct_number,acct_name)
## self.rate = rate
## def addInterest (self):
## interest = self.balance * self.rate
## print("adding interest to the account,",self.rate * 100,"percent")
## self.deposit (interest)
class BankAccount:
def __init__(self, acct_number, acct_name):
self.acct_number = acct_number
self.acct_name = acct_name
self.balance = 0.0
def displayBalance(self):
print( "The account balance is:", self.balance)
def deposit(self, amount):
self.balance = self.balance + amount
print( "You deposited", amount)
print( "The new balance is:", self.balance)
def withdraw(self, amount):
if self.balance >= amount:
self.balance = self.balance - amount
print( "You withdrew", amount)
print( "The new balance is:", self.balance)
else:
print ("You tried to withdraw", amount)
print ("The account balance is:", self.balance)
print("Withdrawl denied. Not enough funds.")
class InterestAccount(BankAccount):
def addInterest(self, rate):
interest = self.balance * rate
print( "adding interest to the account,",rate * 100,"percent")
self.deposit (interest)
myAccount = InterestAccount(234567, "Warren Sande")
print ("Account name:", myAccount.acct_name)
print( "Account number:", myAccount.acct_number)
myAccount.displayBalance()
myAccount.deposit(34.52)
myAccount.addInterest(0.11)