1 __new__方法 创建对象

实质是:调用父类的__new__方法创建的对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---init方法") def __new__(cls):
print("---new方法") dog1 = Dog()
---new方法     #只是创建了类对象,没有初始化,没有实例化对象

  2)版本2: cls此时是Dog指向的那个类对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---init方法") def __new__(cls):   
print("---new方法")
print(id(cls)) print(id(Dog))
dog1 = Dog()
38097176
---new方法
38097176

  3)版本3:调用 被重写的方法__new__  创建对象

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---del方法") def __new__(cls):
print("---new方法") #对父类的方法重写
# print(id(cls))
object.__new__(cls) #继续调用 被重写的方法 创建对象 #print(id(Dog))
dog1 = Dog()
---new方法
---del方法 #没有执行初始化

  4)版本4  __init__ 只负责初始化

class Dog(object):
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法") def __del__(self):
print("---del方法") def __new__(cls): #cls此时是Dog指向的那个类对象
print("---new方法")
# print(id(cls))
return object.__new__(cls) #print(id(Dog))
dog1 = Dog()
---new方法
---init方法
---del方法

  5)版本5:优化版

class Dog(object):
#初始化 对象一创建开始执行
def __init__(self):
print("---init方法") def __str__(self):
print("---str方法")
return "返回对象的描述信息" #对象内存销毁时执行
def __del__(self):
print("---del方法") #通过父类的__new__方法创建对象
def __new__(cls): #cls此时是Dog指向的那个类对象
print("---new方法")
# print(id(cls))
return object.__new__(cls) #print(id(Dog))
dog1 = Dog()

    

相当于做了3件事情

1.调用__new__方法来创建对象,然后找了一个变量来接受__new__的返回值,这个返回值表示创建出来的对象的引用

2. __init__(刚刚创建出来的对象的引用)

3. 返回对象的引用
构造方法
__new__只负责创建
__init__ 只负责初始化 __new__ 和 __init__相当于C语言的构造方法

2. 创建单例对象

  1)什么是单例

      

class Dog(object):
pass dog1 = Dog()
dog2 = Dog()
print(id(dog1))
print(id(dog2))
139888234067784        #不是同一块内存  不是同一个对象
139888234068008

  2)版本2:

class Dog(object):
def __new__(cls):
if xxx :
return object.__new__(cls)
else:
return 上一次创建的对象的引用 dog1 = Dog()
dog2 = Dog()

 

   3)版本3:flag标志

class Dog(object):
__flag = 0
def __new__(cls):
if flag == 0:
flag = 1
return object.__new__(cls)
else:
xxxxx dog1 = Dog()
dog2 = Dog()

  4)版本4:类属性 实现

class Dog(object):
__flag = 0 #类属性 当做标志位 类属性是公有的
__instance = 0 #类属性 存储 上一次创建对象的引用
def __new__(cls): #这是一个类方法,cls指向类对象cls
if cls.__flag == 0:
cls.__flag = 1
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()
     #类方法
@classmethod
def add_num(cls): #cls指向类对象
cls.num += 1
     #实例方法
def __init__(self,new_name):
self.name = new_name #self.name 是实例属性 ,实例对象独有的
#对类属性 +1
Dog.num += 1

  5)版本5:__instance = None

class Dog(object):
__instance = None
def __new__(cls):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()

  6)版本6:查看对应的内存id

class Dog(object):
__instance = None
def __new__(cls):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance dog1 = Dog()
dog2 = Dog()
print(dog1)
print(dog2)
python@ubuntu:~/pythonS6/python基础092$ python3 08-单例对象.py
<__main__.Dog object at 0x7f46d4584828>
<__main__.Dog object at 0x7f46d4584828>

  7)版本7:只初始化一次对象

 class Dog(object):
__instance = None
def __new__(cls,name):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance def __init__(self,new_name):
self.name = new_name dog1 = Dog("tom") #单例对象 name='tom'
print(id(dog1))
print(dog1.name)
dog2 = Dog("jack") #同一对象 令name='jack'
print(id(dog2))
print(dog2.name)
140546425677920
tom
140546425677920
jack #如何让对象都是tom

      

  8)版本8:添加flag

class Dog(object):
__instance = None
__flag = False
def __new__(cls,name):
if cls.__instance == None:
cls.__instance = object.__new__(cls)
return cls.__instance
else:
return cls.__instance def __init__(self,new_name):
if Dog.__flag == False:
self.name = new_name
Dog.__flag = True dog1 = Dog("tom")
print(id(dog1))
print(dog1.name) #对象里面name = “tom”
dog2 = Dog("jack")
print(id(dog2))
print(dog2.name)
139813073586272
tom
139813073586272
tom #只初始化了一次对象

day 7 __new___的更多相关文章

  1. 面向对象的封装、继承和多态特性_python

    一.面向对象的几个特点 面向对象也称为类,拥有下面几个特点 1.封装特性:利用类的__init__(self)构造方法封装对象 构造方法:__init__(self):在生成对象的时候会自动调用 例子 ...

随机推荐

  1. 查看oracle中表的索引

    oracle中表的索引信息存在 user_indexes 和 user_ind_columns 两张表里面, 其中, user_indexes 系统视图存放是索引的名称以及该索引是否是唯一索引等信息, ...

  2. ZooKeeper学习之路 (六)ZooKeeper API的简单使用(二)级联删除与创建

    编程思维训练 1.级联查看某节点下所有节点及节点值 2.删除一个节点,不管有有没有任何子节点 3.级联创建任意节点 4.清空子节点 ZKTest.java public class ZKTest { ...

  3. SpringMVC如何解决POST请求中文乱码问题,GET的又如何处理呢?

    在web.xml中 <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-c ...

  4. disconf实践(四)基于注解的分布式配置文件管理,自动reload

    上一篇讲解了基于xml的自动reload的分布式配置文件管理,这一篇讲解基于注解的自动reload的方式(基于disconf实践二). 1. 修改spring配置文件 <?xml version ...

  5. PAT——1070. 结绳

    给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的绳子又被当成是另一段绳子,可以再次对折去跟另一段绳子串连.每次串连后,原来两段绳子的长度 ...

  6. Android UI【android 仿微信、QQ聊天,带表情,可翻页,带翻页拖动缓冲】

    http://blog.csdn.net/lnb333666/article/details/8546497 如题,这是公司项目的一个功能模块,先上个效果图: 其次大致说说原理: 1,首先判断输入的字 ...

  7. lwip IP address handling 关于 IP 地址的 操作 API接口

    lwip 2.0.3  IP address handling /** * @file * IP address API (common IPv4 and IPv6) */ 1.u32_t ipadd ...

  8. SQL Server 数据库空间使用情况

    GO /****** Object: StoredProcedure [dbo].[SpaceUsed] Script Date: 2017-12-01 11:15:11 ******/ SET AN ...

  9. Oracle 11g R2 RAC with ASM存储迁移--Rman copy&ASM Rebalance(一)

    ASM GROUP-Rman copy迁移 0x00--环境介绍 VMware版本:VMware12pro 主机操作系统:RHEL6.5_64 共享存储使用VMWARE创建共享磁盘文件 数据库版本:O ...

  10. Struts2+AJAX+JQuery 实现用户登入与注册功能

    要求:必备知识:JAVA/Struts2,JS/JQuery,HTML/CSS基础语法:开发环境:MyEclipse 10 关于UI部分请查看下列链接,有详细制作步骤: 利用:before和:afte ...