zoukankan      html  css  js  c++  java
  • day 7 __new___

    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                 #只初始化了一次对象
  • 相关阅读:
    docker 启动redis
    mysql主从库搭建
    云镜象下载地址整理
    linux 命令积累
    canal 踩坑实录---这可能是你看到的最全最简单的canal教程
    数据库查询超级慢,数据库死锁的查看与解决
    微信公众平台开发---建立服务器与微信公众平台的链接
    使用sql更改表的列的数据类型和添加新列和约束
    Mac安装、配置MongoDB
    shell 变量
  • 原文地址:https://www.cnblogs.com/venicid/p/7892147.html
Copyright © 2011-2022 走看看