class Ball:
"""docstring for Ball"""
def bounce(self):
if self.direction == 'down':
self.direction = 'up'
myBall = Ball()
myBall.direction = 'down'
myBall.color = 'green'
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)
class Ball2(object):
"""docstring for Ball2"""
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'
myBall2 = Ball2('red','normal','down')
print("I just created a ball.")
print("My ball is",myBall2.size)
print("My ball is",myBall2.color)
print("My ball's direction is",myBall2.direction)
print("Now I'm going to bounce the ball.")
print()
myBall2.bounce()
print("Now the ball's direction is",myBall2.direction)
class Ball3(object):
"""docstring for Ball3"""
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
myBall3 = Ball3('blue','big','up')
print(myBall3)
class people():
"""docstring for peopel"""
def __init__(self, name):
self.name = name
def outer(self):
print(self.name)
warrens = people("Tom")
warrens.outer()
people.outer(warrens)
class Triangle():
"""docstring for Triangle"""
def __init__(self, width, height):
self.width = width
self.height = height
def getArea(self):
area = self.width * self.height / 2
return area
class Square():
"""docstring for 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 GameObject():
"""docstring for GameObject"""
def __init__(self, name):
self.name = name
def pickUp(self,player):
pass
class Coin(GameObject):
"""docstring for Coin"""
def __init__(self, value):
GameObject.__init__(self,'coin')
self.value = value
def spend(self,buyer,seller):
pass
class HotDog():
"""docstring for 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"
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 addCondiment(self,condiment):
self.condiments.append(condiment)
myHotDog = HotDog()
print(myHotDog.cooked_level)
print(myHotDog.cooked_string)
print(myHotDog.condiments)
print("Now, I'm going to cook the hot dog.")
print("Cooking hot dog for 4 minutes...")
myHotDog.cook(4)
print(myHotDog)
print("Cooking hot dog for 3 more minutes...")
myHotDog.cook(3)
print(myHotDog)
print("What happens if I cook it for 10 more minutes?")
myHotDog.cook(10)
print(myHotDog)
print("Now, I'm going to add some stuff on my hot dog.")
myHotDog.addCondiment("ketchup")
myHotDog.addCondiment("mustard")
print(myHotDog)
"""
你学到什么
什么是对象
属性和方法
什么是类
创建类的一个实例
特殊方法__init__(),__str__()
多态
继承
代码桩
"""
"""
测试题
定义一个新的对象类型时用class关键字
对象的描述是属性
对象的操作是方法
类是对象的定义或蓝图,实例是从这个蓝图建立对象
方法中实例引用通常用self
多态,同一个方法,不同的行为
继承,子类得到父类的属性和方法,还可以有父类没有的属性和方法
"""
class BankAccount():
"""docstring for BankAccount"""
def __init__(self, name, account, balance):
self.name = name
self.account = account
self.balance = balance
def outer(self):
print("The account name:",self.name,
"\nThe account number:",self.account,
"\nThe balance:",self.balance)
myName = input("Enter account name: ")
myAccount = int(input("Enter account number: "))
myBalance = float(input("Enter balance: "))
myAccount = BankAccount(myName,myAccount,myBalance)
myAccount.outer()
class InterestAccount(BankAccount):
"""docstring for InterestAccount"""
def __init__(self, name, account, balance, interest_rate):
BankAccount.__init__(self,name,account,balance)
self.interest_rate = float(interest_rate)
def addInterest(self):
self.balance = self.balance + self.balance * self.interest_rate
def outerbalance(self):
print(self.balance)
myInterestRate = float(input("Enter interest rate: "))
myAccountInterest = InterestAccount(myName,myAccount,myBalance,myInterestRate)
myAccountInterest.outerbalance()
class BankAccount2():
"""docstring for BankAccount2"""
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 withdraw",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.")
myAccount2 = BankAccount2(234,"Warren Sande")
print("Account name:",myAccount2.acct_name)
print("Account number:",myAccount2.acct_number)
myAccount2.displayBalance()
myAccount2.deposit(34.52)
myAccount2.withdraw(12.25)
myAccount2.withdraw(30.18)
class InterestAccount2(BankAccount2):
"""docstring for InterestAccount2"""
def __init__(self, acct_number, acct_name, rate):
BankAccount2.__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)
myAccount2 = InterestAccount2(234,"Warren Sande",0.11)
print("Account name:",myAccount2.acct_name)
print("Account number:",myAccount2.acct_number)
myAccount2.displayBalance()
myAccount2.deposit(34.52)
myAccount2.addInterest()