class Student(object):def __new__(cls, *args, **kwargs):"""这个相当于Java当中的构造函数"""print(cls) # 类本身 <class'__main__.Student'>print('---------new--------------')obj = super().__new__(cls) # __new__创造出了对象print(obj)# <__main__.Student object at 0x00000000021C9C88>print(obj.__dict__) # {}return objdef __init__(self, name, age):"""创建完实例对象之后,才会调用__init__."""print(self)#<__main__.Student object at 0x00000000021C9C88>print('------------init----------')self.name = nameself.age = age
注意:new更像是其他语言当中的构造函数,必须有返回值,返回值实例化的对象,init只是初始化构造函数,必须没有返回
值,仅仅只是初始化功能,并不能 new 创建对象
在实例化对象的时候函数的调用顺序依次是call==>new==>init.
