2.创建对象
字符串、列表名等都是对象
python万物皆对象
定义一个类
class Person(object): def eat(self,food): print('一个人在吃',food) def sleep(self,t): print('一个人睡了',t,'小时') tom = Person() print(tom) tom.eat('饭') tom.sleep(10)
3.动态绑定属性
class Person(object): def eat(self,food): print(self.name,'一个人在吃',food) def sleep(self,t): print(self.name ,'睡了',t,'小时') tom = Person() print(tom) tom.name = 'tom' tom.eat('饭') tom.sleep(10)
4.初始化方法中调用属性
class Lei(object): def __init__(self,name,age): print('init run ...', self) #绑定属性时,使用self.属性名 = 值 self.name = name self.age = age def show(self): print(self.name,self.age) rom = Lei('rom',10) print(rom) print(rom.name) print(rom.age) rom.show() print("________________________________________________________________________________________________________________") jack = Lei('jack',18) print(jack) print(jack.name) print(jack.age) jack.show()
5.__str__方法
5.1 方法 1
class Lei(object): def __init__(self,name,age,height): #绑定属性时,使用self.属性名 = 值 self.name = name self.age = age self.height = height def __str__(self): print("to String run ....",self.name) print(self.name,self.age,self.height) #这个方法必须有个返回值,哪怕是个空串 return "123" rom = Lei('rom',10,'100cm') print(rom) print(rom.name) print(rom.age) print(rom.height) print("________________________________________________________________________________________________________________") jack = Lei('jack',18,'1800cm') print(jack) print(jack.name) print(jack.age) print(jack.height)
5.2 方法2
class Lei(object): def __init__(self,name,age,height): #绑定属性时,使用self.属性名 = 值 self.name = name self.age = age self.height = height def __str__(self): st = self.name.ljust(6)+str(self.age).ljust(5)+self.height.ljust(10) #这个方法必须有个返回值,哪怕是个空串 return st rom = Lei('rom',10,'100cm') print(rom) print(rom.name) print(rom.age) print(rom.height) print("________________________________________________________________________________________________________________") jack = Lei('jack',18,'1800cm') print(jack) print(jack.name) print(jack.age) print(jack.height)
6.del方法
回收资源,在没有对象引用时,计数器为0,del方法被调用
class Lei(object): def __init__(self,name,age,height): #绑定属性时,使用self.属性名 = 值 self.name = name self.age = age self.height = height def __str__(self): st = self.name.ljust(6)+str(self.age).ljust(5)+self.height.ljust(10) #这个方法必须有个返回值,哪怕是个空串 return st def __del__(self): print("del 被调用") rom = Lei('rom',10,'100cm') del rom print('over')
7.案例练习
7.1烤地瓜
class SweetPotato(object): #实现初始化方法,初始地瓜状态,和总烧烤时间 def __init__(self): self.status = '生瓜蛋子' self.total_time = 0 #实现一个烧烤方法 #该方法有一个烧烤时间,这个时间会被累积到总时间上 #判断总时间,来改变地瓜状态 def cook(self,t): #累加时间 self.total_time += t #判断 时间来改变地瓜状态 if self.total_time >=1 and self.total_time < 3: self.status = "还是生的" elif self.total_time >=3 and self.total_time < 6: self.status = "半生不熟" elif self.total_time >=6 and self.total_time < 8: self.status = "烤好了" elif self.total_time >=8 and self.total_time < 10: self.status = "烤糊了" else: self.status = "成炭了" #显示地瓜对象 def __str__(self): s = self.status + f'被烤了 {self.total_time}' + '分钟' return s sp1 = SweetPotato() print(sp1) sp1.cook(2) print(sp1) sp1.cook(2) print(sp1) sp1.cook(2) print(sp1) sp1.cook(2) print(sp1) sp1.cook(2) print(sp1)
7.2 人狗案例
class Dog(object): def __init__(self,name,age): self.name = name self.age = age def bark(self,n): for i in range(n): print("wowowo!") class Person(object): def __init__(self,name,age): self.name = name self.age = age def add_pet(self,pet): self.pet = pet def listen_dog_bark(self,n): self.pet.bark(n) tom = Person('tom',20) tom.add_pet(Dog('旺财',5)) tom.listen_dog_bark(2)
7.3 存放家具
class Furniture(object): def __init__(self,name,area): self.name = name self.area = area class House(object): def __init__(self,address,area): self.address = address self.area = area #只有百分之七十可以放家具 self.free_area = area * 0.7 self.ocp = 0 self.Fur = list() def add_funiture(self,F): if self.free_area - ( self.ocp + F.area) >= 0: self.ocp += F.area self.Fur.append(F.name) else: print("不好意思,该家具太大了,不能添加") def check_having_fur_and_space(self): if len(self.Fur) == 0: print("you do not have any FUR") else: print("fur: ",self.Fur) print("having space:",self.free_area-self.ocp) home1 = House("beijing",100) home1.check_having_fur_and_space() print("____________________________________________________________________________") home1.add_funiture(Furniture('电视',10)) home1.check_having_fur_and_space() home1.add_funiture(Furniture('桌子',15)) home1.check_having_fur_and_space() home1.add_funiture(Furniture('床',20)) home1.check_having_fur_and_space() home1.add_funiture(Furniture('电脑',10)) home1.check_having_fur_and_space() home1.add_funiture(Furniture('沙发',20)) home1.check_having_fur_and_space() home1.add_funiture(Furniture('衣柜',15)) home1.check_having_fur_and_space()
结语
点个赞吧!!!求求求了!!!!