python语言学习笔记(四)-----面向对象1
2018-01-16 16:45
711 查看
class SweetPotato: def __init__(self): self.cookedlevel=0 self.cookedstring='生的' self.condinments=[] def __str__(self): return '地瓜的状态{}({}),加入的配料有{}'.format(self.cookedstring,self.cookedlevel,self.condinments) def cook(self,cook_time): self.cookedlevel+=cook_time if self.cookedlevel>=0 and self.cookedlevel<=3: self.cookedstring='生的' elif self.cookedlevel>3 and self.cookedlevel<=5: self.cookedstring='半生不熟' elif self.cookedlevel>5 and self.cookedlevel<=8: self.cookedstring='熟了' elif self.cookedlevel>8 : self.cookedstring='糊了' def add_condiments(self,item): self.condinments.append(item) digua=SweetPotato() digua.cook(1) print(digua) digua.cook(1) print(digua) digua.cook(1) print(digua) digua.cook(1) print(digua) digua.add_condiments('甘梅粉') digua.cook(1) print(digua) digua.add_condiments('芝士粉') digua.cook(1) print(digua) digua.cook(1) print(digua) digua.cook(1) print(digua) digua.cook(1) print(digua) digua.cook(1) print(digua)
地瓜的状态生的(1),加入的配料有[] 地瓜的状态生的(2),加入的配料有[] 地瓜的状态生的(3),加入的配料有[] 地瓜的状态半生不熟(4),加入的配料有[] 地瓜的状态半生不熟(5),加入的配料有['甘梅粉'] 地瓜的状态熟了(6),加入的配料有['甘梅粉', '芝士粉'] 地瓜的状态熟了(7),加入的配料有['甘梅粉', '芝士粉'] 地瓜的状态熟了(8),加入的配料有['甘梅粉', '芝士粉'] 地瓜的状态糊了(9),加入的配料有['甘梅粉', '芝士粉'] 地瓜的状态糊了(10),加入的配料有['甘梅粉', '芝士粉']
案例(存放家具):两种方式
class Home(): def __init__(self,new_area,new_info,new_addr): self.area=new_area self.info=new_info self.addr=new_addr self.left_area=new_area self.contain_items=[] def __str__(self): return '房子的总面积是{},{},地址:{},包含的家具有{},剩余面积为{}'.format(self.area,self.info,self.addr,self.contain_items,self.left_area) def add_furniture(self,item): self.contain_items.append(item.info) self.left_area-=item.area class Bed(): def __init__(self,new_area,new_info): self.area=new_area self.info=new_info def __str__(self): return '{},面积为{}'.format(self.info,self.area) my_home=Home(111,'四室两厅','双流航空港') bed1=Bed(4,'席梦思') print(bed1) my_home.add_furniture(bed1) print(my_home)
class Home(): def __init__(self,new_area,new_info,new_addr): self.area=new_area self.info=new_info self.addr=new_addr self.left_area=new_area self.contain_items=[] def __str__(self): return '房子的总面积是{},{},地址:{},包含的家具有{},剩余面积为{}'.format(self.area,self.info,self.addr,self.contain_items,self.left_area) def add_furniture(self,item): self.contain_items.append(item.get_info()) self.left_area-=item.get_area() class Bed(): def __init__(self,new_area,new_info): self.area=new_area self.info=new_info def __str__(self): return '{},面积为{}'.format(self.info,self.area) def get_area(self): return self.area def get_info(self): return self.info my_home=Home(111,'四室两厅','双流航空港') bed1=Bed(4,'席梦思') print(bed1) my_home.add_furniture(bed1) print(my_home)
席梦思,面积为4 房子的总面积是111,四室两厅,地址:双流,包含的家具有['席梦思'],剩余面积为107
相关文章推荐
- Dive in Python学习笔记五:对象和面向对象
- python 学习笔记-面向对象的理解
- Python学习笔记三:面向对象
- Java语言基础学习笔记——面向对象
- Python 学习笔记 - 面向对象(封装,继承和多态)
- python语言学习笔记(二)------判断输入密码强弱
- Python学习笔记之面向对象
- Python 【面向对象(类)】 学习笔记
- 数据结构与算法:python语言描述学习笔记Part3_1
- 数据结构与算法:python语言描述学习笔记Part4_kmp算法及改进
- Python学习笔记016——面向对象
- Python语言学习笔记
- python学习笔记(三)面向对象
- Python 学习笔记 - 面向对象(其他)
- Python语言学习笔记之基本语法
- 数据结构与算法:python语言描述学习笔记Part2
- python语言学习笔记
- Python 学习笔记 - 面向对象(特殊成员)
- Python学习笔记----面向对象
- python学习笔记——语言分类