day2 self __init__ __str__
1 self 谁调用指向谁自己 相当于其他语言的this
#1.类名
class Cat(): #大驼峰的命名规范
#2.类的属性 #3.类的方法
def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
#第二种 获取对象的属性
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #self #创建一个对象
tom = Cat() #调用tom指向的对象中的 方法
tom.eat()
tom.run() #给tom指向的对象添加2个属性
tom.name = "汤姆"
tom.age = 18 tom.instroduce() #相当于 tom.instroduce(tom) lanmao = Cat()
lanmao.name = "蓝猫"
lanmao.age = 13
lanmao.instroduce()


2 .__init__方法 魔法方法
class Cat():
'''定义1个Cat类'''
def __init__(self): #python解释器自动执行__init__方法
print("---haha----")
def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #创建一个对象
tom = Cat()
tom.eat()
tom.run()
tom.name = "汤姆"
tom.age = 18
tom.instroduce() lanmao = Cat()
lanmao.name = "蓝猫"
lanmao.age = 13
lanmao.instroduce()

3 .__init__方法的流程
class Cat():
'''定义1个Cat类'''
#初始化一个对象
def __init__(self,new_name,new_age):
self.name = new_name
self.age = new_age def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #创建一个对象
tom = Cat("汤姆",33)
tom.eat()
tom.run()
#tom.name = "汤姆"
#tom.age = 18
tom.instroduce() lanmao = Cat("蓝猫",22)
#lanmao.name = "蓝猫"
#lanmao.age = 13
lanmao.instroduce()

4.__str__方法 魔法方法 获取对象的描述信息
调用的是对象里面的name,age
1)版本1:
class Cat():
'''定义1个Cat类'''
def __init__(self,new_name,new_age):
self.name = new_name
self.age = new_age def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #创建一个对象
tom = Cat("汤姆",33) lanmao = Cat("蓝猫",22) print(tom)
print(lanmao) ####执行结果
python@ubuntu:~/pythonS6/python基础07$ python3 10-__str__.py
<__main__.Cat object at 0x7f2030af3978>
<__main__.Cat object at 0x7f2030af39b0>
2)版本2:
class Cat():
'''定义1个Cat类'''
def __init__(self,new_name,new_age):
self.name = new_name
self.age = new_age def __str__(self):
return("hahah") def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #创建一个对象
tom = Cat("汤姆",33) lanmao = Cat("蓝猫",22) print(tom)
print(lanmao) ### 结果
hahah
hahah
3)版本3:
class Cat():
'''定义1个Cat类'''
def __init__(self,new_name,new_age):
self.name = new_name
self.age = new_age ## 获取对象的描述信息
def __str__(self):
return("%s的年龄是%d"%(self.name,self.age)) #调用的是对象内存中的name,age def eat(self):
print("我在吃东西。。。")
def run(self):
print("我在跑步")
def instroduce(self):
print("%s的年龄是%d"%(self.name,self.age)) #创建一个对象
tom = Cat("汤姆",33) lanmao = Cat("蓝猫",22) print(tom)
print(lanmao) #### 执行结果
汤姆的年龄是33
蓝猫的年龄是22

5.全局变量,函数 和属性,方法的区别

6.小应用:烤地瓜
1)版本1:
class SweetPotato:
def __init__(self):
self.cookedString = '生的'
self.cookedLevel = 0 def __str__(self):
return "地瓜状态:%s(%d)"%(self.cookedString,self.cookedLevel) def cook(self,cooked_time):
self.cookedLevel += cooked_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 = "烤糊了" #创建一个对象
di_gua = SweetPotato()
print(di_gua) #开始烤地瓜
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
地瓜状态:生的(0)
地瓜状态:生的(1)
地瓜状态:生的(2)
地瓜状态:半生不熟(3)
地瓜状态:半生不熟(4)
地瓜状态:熟了(5)
地瓜状态:熟了(6)
地瓜状态:熟了(7)

2)版本2:添加作料
class SweetPotato:
def __init__(self):
self.cookedString = '生的'
self.cookedLevel = 0
self.add_list = [] def __str__(self):
return "地瓜状态:%s(%d)添加了%s"%(self.cookedString,self.cookedLevel,str(self.add_list)) def cook(self,cooked_time):
self.cookedLevel += cooked_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(self,add_something):
self.add_list.append(add_something) #创建一个对象
di_gua = SweetPotato()
print(di_gua) #开始烤地瓜
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
di_gua.add("葱")
print(di_gua)
di_gua.cook(1)
di_gua.add("蒜")
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
di_gua.add("酱")
print(di_gua)
di_gua.cook(1)
print(di_gua)
地瓜状态:生的(0)添加了[]
地瓜状态:生的(1)添加了[]
地瓜状态:生的(2)添加了[]
地瓜状态:半生不熟(3)添加了['葱']
地瓜状态:半生不熟(4)添加了['葱', '蒜']
地瓜状态:熟了(5)添加了['葱', '蒜']
地瓜状态:熟了(6)添加了['葱', '蒜', '酱']
地瓜状态:熟了(7)添加了['葱', '蒜', '酱']
3)版本3:优化版
class SweetPotato:
'''定义了一个地瓜类'''
def __init__(self):
self.cookedString = '生的'
self.cookedLevel = 0
self.add_list = [] #为了能够存储多个数据,往往在开发中让一个属性是列表list def __str__(self):
return "地瓜状态:%s(%d)添加了%s"%(self.cookedString,self.cookedLevel,str(self.add_list)) def cook(self,cooked_time):
#因为这个方法被调用了多次,为了能够再一次调用这个方法的时候 能够获取到上一次调用这个方法的cooked_time
#所以需要在此,把cooked_time保存到这个对象的属性中,因为属性不会随着方法的调用而结束(一个方法被调用的时候是可以用局部变量来保存数据的,但是当这个方法定义结束之后这个方法中的所有数据就没有了)
self.cookedLevel += cooked_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(self,add_something):
self.add_list.append(add_something) #创建一个对象
di_gua = SweetPotato()
print(di_gua) #开始烤地瓜
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
di_gua.add("葱")
print(di_gua)
di_gua.cook(1)
di_gua.add("蒜")
print(di_gua)
di_gua.cook(1)
print(di_gua)
di_gua.cook(1)
di_gua.add("酱")
print(di_gua)
di_gua.cook(1)
print(di_gua)
7.小应用:存放家居
1)版本1:
#1.定义房子类
class Home:
def __init__(self,new_area,new_info,new_addr):
self.area = new_area
self.info = new_info
self.addr = new_addr def __str__(self):
return "房子面积是:%d,户型是:%s,地址是:%s"%(self.area,self.info,self.addr) #2,创建对象房子
fangzi = Home(200,"三室一厅","北京朝阳区")
print(fangzi)
2)版本2:创建床类
#1.定义房子类
class Home:
def __init__(self,new_area,new_info,new_addr):
self.area = new_area
self.info = new_info
self.addr = new_addr def __str__(self):
return "房子面积是:%d,户型是:%s,地址是:%s"%(self.area,self.info,self.addr) class Bed:
'''定义床类'''
def __init__(self,new_name,new_area):
self.name = new_name
self.area = new_area def __str__(self):
return "%s占用大面积是%d"%(self.name,self.area) #2,创建对象房子
fangzi = Home(200,"三室一厅","北京朝阳区")
print(fangzi) #创建床对象
bed1 = Bed("双人床",5)
print(bed1)
3)版本3:添加床到房子里面
#1.定义房子类
class Home:
def __init__(self,new_area,new_info,new_addr):
self.area = new_area
self.info = new_info
self.addr = new_addr def __str__(self):
message = "房子面积是:%d,户型是:%s,地址是:%s"%(self.area,self.info,self.addr)
message += "剩余面积是%s"%self.area
return message def add(self,item):
self.area -= item.area class Bed:
'''定义床类'''
def __init__(self,new_name,new_area):
self.name = new_name
self.area = new_area def __str__(self):
return "%s占用大面积是%d"%(self.name,self.area) #2,创建对象房子
fangzi = Home(200,"三室一厅","北京朝阳区")
print(fangzi) #创建床对象
bed1 = Bed("双人床",5)
print(bed1) fangzi.add(bed1)
print(fangzi)
房子面积是:200,户型是:三室一厅,地址是:北京朝阳区剩余面积是200
双人床占用大面积是5
房子面积是:195,户型是:三室一厅,地址是:北京朝阳区剩余面积是195
4)版本4:打印房子的物品
#1.定义房子类
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):
message = "房子面积是:%d,剩余面积%s,户型是:%s,地址是:%s,物品有%s"%(self.area,self.left_area,self.info,self.addr,str(self.contain_items))
return message def add(self,item):
self.left_area -= item.area
self.contain_items.append(item.name) class Bed:
'''定义床类'''
def __init__(self,new_name,new_area):
self.name = new_name
self.area = new_area def __str__(self):
return "%s占用大面积是%d"%(self.name,self.area) #2,创建对象房子
fangzi = Home(200,"三室一厅","北京朝阳区")
print(fangzi) #创建床对象
bed1 = Bed("双人床",5)
print(fangzi) fangzi.add(bed1)
print(fangzi)
房子面积是:200,剩余面积200,户型是:三室一厅,地址是:北京朝阳区,物品有[]
房子面积是:200,剩余面积200,户型是:三室一厅,地址是:北京朝阳区,物品有[]
房子面积是:200,剩余面积195,户型是:三室一厅,地址是:北京朝阳区,物品有['双人床']

5)版本5:推荐调用bed里面方法
房子面积是:200,剩余面积200,户型是:三室一厅,地址是:北京朝阳区,物品有[]
房子面积是:200,剩余面积200,户型是:三室一厅,地址是:北京朝阳区,物品有[]
房子面积是:200,剩余面积200,户型是:三室一厅,地址是:北京朝阳区,物品有[]
房子面积是:200,剩余面积195,户型是:三室一厅,地址是:北京朝阳区,物品有['双人床']
房子面积是:200,剩余面积186,户型是:三室一厅,地址是:北京朝阳区,物品有['双人床', '架子床']
#1.定义房子类
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):
message = "房子面积是:%d,剩余面积%s,户型是:%s,地址是:%s,物品有%s"%(self.area,self.left_area,self.info,self.addr,str(self.contain_items))
return message def add(self,item):
# self.left_area -= item.area
#self.contain_items.append(item.name)
self.left_area -= item.get_area() #推荐用bed的方法
self.contain_items.append(item.get_name()) class Bed:
'''定义床类'''
def __init__(self,new_name,new_area):
self.name = new_name
self.area = new_area def __str__(self):
return "%s占用大面积是%d"%(self.name,self.area) def get_name(self):
return self.name def get_area(self):
return self.area
#2,创建对象房子
fangzi = Home(200,"三室一厅","北京朝阳区")
print(fangzi) #创建床对象
bed1 = Bed("双人床",5)
print(fangzi) bed2 = Bed("架子床",9)
print(fangzi) fangzi.add(bed1)
print(fangzi) fangzi.add(bed2)
print(fangzi)

day2 self __init__ __str__的更多相关文章
- python中魔法方法__init__,__str__,__del__的详细使用方法
1. python中的魔法方法, 类似__init__, __str__等等,这些内置好的特定的方法进行特定的操作时会自动被调用 2. __init__的使用方法 class 类名(object): ...
- python__基础 : 类的__init__,__str__,__del__方法
__init__:当实例化一个类的时候,首相会执行__new__方法创建一个对象,接下来会执行__init__方法对对象的一些属性进行初始化. 所以如果对象有属性,一般会直接写在__init__方法里 ...
- miniproject black jack--Fail
第一部分 下载这个小项目的程序模板并回顾card类的定义.这个类已经执行了所以你的任务是自己熟悉下代码.开始,通过粘贴card类定义到程序模板中并验证我们的代码如预期那样工作. 实现“__init__ ...
- Understanding Python metaclasses
转载:https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ None of the existing article ...
- 理解Python元类(转)
add by zhj:先收藏了,有时间看,图倒是不少,可以配合stackover flow上那篇文章一起看 原文:http://blog.ionelmc.ro/2015/02/09/understan ...
- Python元类实践--自己定义一个和collections中一样的namedtuple
大家可能很熟悉在collections模块中有一个很好用的扩展数据类型-namedtuple. 如果你还不知道这个类型,那么请翻看标准手册. 我利用元类轻松定义一个namedtuple. 先把代码贴上 ...
- Python面试题(练习二)
1.用Python实现一个二分查找的函数. data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def ...
- python部分 + 数据库 + 网络编程
PS:附上我的博客地址,答案中略的部分我的博客都有,直接原标题搜索即可.https://www.cnblogs.com/Roc-Atlantis/ 第一部分 Python基础篇(80题) 为什么学习P ...
- Python3 面向对象进阶2
目录 Classmethod Staticmethod Isinstance Issubclass 反射 概念 hasattr getattr setattr delattr 魔法方法 概念 __ne ...
随机推荐
- Requests中文乱码解决方案
分析: r = requests.get(“http://www.baidu.com“) **r.text返回的是Unicode型的数据. 使用r.content返回的是bytes型的数据. 也就是说 ...
- A blog about Core Animation and other iOS graphics framework
A blog about Core Animation and other iOS graphics frameworks. https://www.calayer.com/
- BZOJ1563:[NOI2009]诗人小G(决策单调性DP)
Description Input Output 对于每组数据,若最小的不协调度不超过1018,则第一行一个数表示不协调度若最小的不协调度超过1018,则输出"Too hard to arr ...
- C++内存总结——开坑,随时总结添加
C++内存区域分为: 程序代码区:存储程序代码的地方 栈区:编译器自动管理(分配释放)的内存区域,如函数参数,函数中的局部变量 堆区(又称动态存储区):由C语言中的函数malloc和free和C++ ...
- [19/04/30-星期二] GOF23_行为型模式(中介者模式、命令模式、解释器模式、访问者模式)
一.中介者模式(meditor) [中介] /*** * 抽象中介者接口和其具体实现类"经理"类 */ package cn.sxt.meditor; import java.ut ...
- 4.12 Spark环境更新
addJar方法是做什么的呢?它用于将Jar文件添加到Driver的RPC环境中. 通过addJar和addFile可以将各种任务执行所依赖的文件添加到Driver的RPC环境中, •小结 伴生对象是 ...
- python中动态导入模块
当导入的模块不存在时,就会报ImportError错误,为了避免这种错误可以备选其他的模块或者希望优先使用某个模块或包,可以使用try...except...导入模块或包的方式. 例如: Python ...
- 算法——(4)哈希、hashmap、hashtable
1. Hash 把任意长度的输入通过散列算法,变换成固定长度的输出,该输出就是散列值.拥有四个特性: 1. 拥有无限的输入域和固定大小的输出域 2. 如果输入值相同,返回值一样 3. 如果输入值不相同 ...
- PL/SQL Developer编码格式设置
通过PL/SQL中文字段显示乱码或者导出数据出现乱码,原因是数据库的编码格式和PL/SQL的编码格式不统一导致. 查看ORACLE数据库字符集: select userenv('language') ...
- 翻译 TI SerialBLEbridge V 1.4.1
原文地址:http://processors.wiki.ti.com/index.php/SerialBLEbridge_V_1.4.1 Sample App Overview This page d ...