#父与子的编程之旅#第十四章
对象
1、什么是对象?具有属性和方法(能够对对象做的操作)的某个实例可以称为对象。如,一个球,属性有球的颜色,球的大小,球的重量,方法有球的运动方向;
2、属性和方法。如1中所述,属性很好理解,就是能够描述对象特征的一些变量,方法可以理解为一些代码块,这些代码块可以对对象完成一些操作,和我们平时讲的函数有些相似;
3、什么是类?对象的描述(将要让这个对象做什么实现什么功能)或蓝图称为一个类;
4、创建类的一个实例。类的定义只是写了一个框架,将有什么样的属性,将要实现什么样的功能,都是空谈,只有使用类来建立一个真正的对象,类才会发挥真正的作用,该对象就称为这个类的实例。
5、特殊方法__init__()和__str__()。只要创建该类的一个新实例,就要运行__init__()方法,该方法可以向实例的属性传递自己希望的值,完成初始化。__str__()可以按照自己的想法打印类中 的内容,
如:def __str__(self):
msg = "Hi,I'm a "+self.size+" "+self.color+" ball!"
return msg
6、多态。同一个方法,不同的行为。同一个函数名的方法,用在不同的类中,实现的功能不同,称为多态。
7、继承。向父类学习。父类有的属性和方法子类也同样有,子类还可以新增自身的属性和方法,提高代码的复用率。
如:class GameObject:
def __init__(self,name):
self.name = name
def pickUp(self,player):
pass
class Coin(GameObject):#继承GameObject类
def __init__(self,value):
GameObject.__init__(self,"coin")#继承初始化方法并添加新的属性
self.value = value
def spend(self,buyer,seller):#添加新的方法
pass
8、Python中的关键字pass可以作为一个占位符,一时半会想不起来写什么内容的时候,先占位这,这样函数方法模块不会报错
#动手试一试T1 '''为BankAccount建立一个类定义。它应该有一些属性,包括账户名(一个字符串)、账号(一个字符串或整数)和余额(一个浮点数), 另外还要有一些方法显示余额、存钱和取钱''' class BankAccount: def __init__(self,account_name,account_number): self.account_name = account_name self.account_number = account_number self.account_overage = 0.0 def ShowOverage(self): print "The account balance is ",self.account_overage def Save(self,money): self.account_overage = self.account_overage + money print "You save ",money print "The new overage is ",self.account_overage def Withdrawal(self,money): if self.account_overage >= money: self.account_overage = self.account_overage - money print "You withdrawal ",money print "The new overage is ",self.account_overage else: print "You tried to withdrawal",money print "Your balance is",self.account_overage print "No enough funds" myAccount = BankAccount("weimiaomiao", 123456) print "Account name", myAccount.account_name print "Account number", myAccount.account_number myAccount.ShowOverage() myAccount.Save(100) myAccount.Withdrawal(20.5) myAccount.Withdrawal(50) #动手试一试T2 '''建立一个可以挣利息的类,名为InterestAccount。这应当是BankAccount的一个子类(所以会继承BankAccount的属性和方法)。InternetAccount还 应当有一个对应利息率的属性,另外有一个方法来增加利息。为了力求简单,假设每年会调用一次addInternet()方法计算利息并更新余额''' class BankAccount: def __init__(self,account_name,account_number): self.account_name = account_name self.account_number = account_number self.account_overage = 0.0 def ShowOverage(self): print "The account balance is ",self.account_overage def Save(self,money): self.account_overage = self.account_overage + money print "You save ",money print "The new overage is ",self.account_overage def Withdrawal(self,money): if self.account_overage >= money: self.account_overage = self.account_overage - money print "You withdrawal ",money print "The new overage is ",self.account_overage else: print "You tried to withdrawal",money print "Your balance is",self.account_overage print "No enough funds" class InterestAccount(BankAccount): def __init__(self, interest_rate): BankAccount.__init__(self, "weimiaomiao", 123456)#继承BankAccount类 self.interest_rate = interest_rate def addInterest(self): interest = self.account_overage * self.interest_rate print "Your interest is",interest print "Your overall funds is",interest+self.account_overage myAccount = InterestAccount(0.2) myAccount.Save(100) myAccount.Withdrawal(20.5) myAccount.Withdrawal(50) myAccount.addInterest()