1、举例:可以拿球举例,可以操作一个球,比如捡球、抛球、踢球、或者充气,这些称之为动作,还可以通过指出球的颜色、大小和重量来描述一个球。这些称为一个球的属性。
2、对象包括两个方面:可以对他做什么(动作):方法;如何描述(属性或特性):属性
3、在python 中,一个对象的特征(或你知道的事情),称为属性,动作(能够对对象做的操作)称为方法。
球的属性:ball.color ball.size ball.weight 球的方法:ball.kick() ball.throw() ball.inflat()
4、什么是属性:属性就是你所知道关于球的所有方面,球的属性就是一些信息(数字,字符串等),其实就是变量,包含在对象中的变量。
print ball.size ball.color = "red" mycolor = ball.color myball.color = ball.color
5、什么是方法:方法就是可以对对象进行的操作,他们是一些代码块,可以调用代码块完成一些工作,方法就是包含在对象里的函数。
6、创建对象的2个步骤:1、第一步:定义对象看上去什么样,会做什么,也就是它的属性和方法。只是创建一个蓝图,对象的描述和蓝图称为一个类。
2、第二步:使用类来建立一个真正的对象。这个对象称为这个类的一个实例。
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 ") myball.bounce() # 使用一个方法 print("Now the ball's direction is ",myball.direction)
7、初始化对象:名为__init__
class ball: def __init__(self,color,size,direction): # 这里__init__ 为两条下划线 self.color = color self.size = size self.direction = direction def bounce(self): if self.direction == "down": self.direction == "up" myball = ball("red","small","down") # 属性作为__int__()的参数传入 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 myball.bounce() print("Now the ball's direction is ",myball.direction)
8、__str__(),它会告诉python打印(print)一个对象时具体显示什么内容。
# 使用__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)
9、self self参数会告诉方法哪个对象调用它,称为实例引用
10、热狗程序
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 = "Charocal" 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 : ",mydog.cooked_level) # 创建一个实例,名为hotdog 烤前的状态 print("mydog cooked_string : ",mydog.cooked_string) print("mydog condiments : " , mydog.condiments) print("i'm now going to cook the hot dog") mydog.cook(4) # 用cook()方法,打印烤后的状态 print("mydog cookde_level : ",mydog.cooked_level) print("mydog cooked_string : ",mydog.cooked_string)
11、包含cook(),add_condiment()和__str__()的Hot dog类
class Hotdog: def __init__(self): # 初始化对象 self.cooked_level = 0 self.cooked_string = "Raw" self.condiments = [] def __str__(self): #定义新的__str__ 方法 () 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 + "." # __str__ 用msg 作为返回 更好地打印我们的对象 return msg def cook(self,time): self.cooked_level = self.cooked_level + time if self.cooked_level > 8: self.cooked_string = "Charocal" 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 minutes ...") mydog.cook(3) print(mydog) print("what happens if i cook it for 10 more minutes ? ") mydog.cook(10) print("now , i'm going to add some stuff on my hot dog ") mydog.addCondiment("Ketchuo") mydog.addCondiment("mustard") print (mydog)
12、对象的多态性
# 对象的多态性 -同一个方法,不同的行为 class Triangle: def __init__(self,width,height): self.width = width self.height = height def getArea(self): area = self.width * self.width / 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) # 创建2个 实例 mySqure = Square(7) myTriangle.getArea() print(myTriangle) mySqure.getArea() print(mySqure) # 如何用__str__() 方法正常打印输出?
13、对象的继承性
对象的继承性 class Gameobject: def __init__(self,name): self.name = name def pickup(self): pass # put code here to add the project # to the player's collection class Coin(Gameobject): def __init__(self,value): Gameobject.__init__(self,value) self.value = value def spend(self,buyer,seller): pass # put code here to remove the coin # from the buyer's money and # add it to the seller's money
14、课后习题
# 为BankAccount 建立一个类定义。它有的属性为:账户名(字符串),账号(一个字符串或整数),余额(浮点数),另外还需用方法显示余额、存钱、取钱 # 作者:wang cheng hua 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 depositde ",amount) print("the new balance is ",self.balance) def withdraw(self,amount): if self.balance >= amount: self.balance =self.balance -amount print("you withdrawde ",amount) print("the new balance ",self.balance) else: print("you have tried to withdraw ",amount) print(" the account balance is ",self.balance) print("withdrawal denied . not enough funds ") myaccount =BankAccount('440303201','wang') myaccount.displayBalance() myaccount.deposit(500) myaccount.withdraw(300) myaccount.withdraw(300) # 为BankAccount 建立一个利息的类,名为interestAccount ,为BanKAccount 的子类 (会继承BankAccount 的属性和方法) # 作者:wang cheng hua class InterestAccount(BankAccount): def __init__(self,acct_number,acct_name,rate): BankAccount.__init__(self,acct_number,acct_name) self.rate = rate def addInterest(self): interst = self.balance * self.rate print("adding the interst in the account ,",self.rate * 100,"precent") self.deposit(interst) myaccount = InterestAccount("230333","wang",0.11) print("the account number is : ",myaccount.acct_number) print("the account name is :",myaccount.acct_name) myaccount.displayBalance() myaccount.deposit(34.52) myaccount.addInterest()