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__的更多相关文章

  1. python中魔法方法__init__,__str__,__del__的详细使用方法

    1. python中的魔法方法, 类似__init__, __str__等等,这些内置好的特定的方法进行特定的操作时会自动被调用 2. __init__的使用方法 class 类名(object):  ...

  2. python__基础 : 类的__init__,__str__,__del__方法

    __init__:当实例化一个类的时候,首相会执行__new__方法创建一个对象,接下来会执行__init__方法对对象的一些属性进行初始化. 所以如果对象有属性,一般会直接写在__init__方法里 ...

  3. miniproject black jack--Fail

    第一部分 下载这个小项目的程序模板并回顾card类的定义.这个类已经执行了所以你的任务是自己熟悉下代码.开始,通过粘贴card类定义到程序模板中并验证我们的代码如预期那样工作. 实现“__init__ ...

  4. Understanding Python metaclasses

    转载:https://blog.ionelmc.ro/2015/02/09/understanding-python-metaclasses/ None of the existing article ...

  5. 理解Python元类(转)

    add by zhj:先收藏了,有时间看,图倒是不少,可以配合stackover flow上那篇文章一起看 原文:http://blog.ionelmc.ro/2015/02/09/understan ...

  6. Python元类实践--自己定义一个和collections中一样的namedtuple

    大家可能很熟悉在collections模块中有一个很好用的扩展数据类型-namedtuple. 如果你还不知道这个类型,那么请翻看标准手册. 我利用元类轻松定义一个namedtuple. 先把代码贴上 ...

  7. Python面试题(练习二)

    1.用Python实现一个二分查找的函数. data = [1, 3, 6, 7, 9, 12, 14, 16, 17, 18, 20, 21, 22, 23, 30, 32, 33, 35] def ...

  8. python部分 + 数据库 + 网络编程

    PS:附上我的博客地址,答案中略的部分我的博客都有,直接原标题搜索即可.https://www.cnblogs.com/Roc-Atlantis/ 第一部分 Python基础篇(80题) 为什么学习P ...

  9. Python3 面向对象进阶2

    目录 Classmethod Staticmethod Isinstance Issubclass 反射 概念 hasattr getattr setattr delattr 魔法方法 概念 __ne ...

随机推荐

  1. BZOJ 1002 轮状病毒 矩阵树定理

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1002 题目大意: 给定n(N<=100),编程计算有多少个不同的n轮状病毒 思路 ...

  2. 关于numpy mean函数的axis参数

    import numpy as np X = np.array([[1, 2], [4, 5], [7, 8]]) print np.mean(X, axis=0, keepdims=True) pr ...

  3. 验证码帮助类【CaptchaHelper 】

    GDI+:Graphics Device Interface Plus也就是图形设备接口,提供了各种丰富的图形图像处理功能;在C#.NET中,使用GDI+处理二维(2D)的图形和图像,使用Direct ...

  4. 闲话缓存:ZFS 读缓存深入研究-ARC(一)

    在Solaris ZFS 中实现的ARC(Adjustable Replacement Cache)读缓存淘汰算法真是很有意义的一块软件代码.它是基于IBM的Megiddo和Modha提出的ARC(A ...

  5. 解决apache启动错误:Could not reliably determine the server's fully qualified domain name

    启动apache遇到错误:httpd: Could not reliably determine the server's fully qualified domain name [root@serv ...

  6. 用JavaScript编写气泡

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 原生js创建模态框

    1.效果图如下: 2.代码如下: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...

  8. AFNetworking 2.x 的SSL身份认证

    一般来讲如果app用了web service , 我们需要防止数据嗅探来保证数据安全.通常的做法是用ssl来连接以防止数据抓包和嗅探 其实这么做的话还是不够的.我们还需要防止中间人攻击(不明白的自己去 ...

  9. PHP实现验证码功能

    原文链接:http://www.qqdeveloper.com/a/54.html 什么是验证码? 借用百度的解释:验证码这个词最早是在2002年由卡内基梅隆大学的路易斯·冯·安.Manuel Blu ...

  10. mongo数据集合属性中存在点号(.)

    基本知识点: 1.似乎mongo3.6之前不允许插入带点(.)或美元符号($)的键,但是当我使用mongoimport工具导入包含点的JSON文件时,它工作正常. 2.在使用spring-data-m ...