class Programer(object):
def __new__(cls, *args, **kwargs): # 创建类的对象
print('call __new__method')
print(args)
return super(Programer, cls).__new__(cls)
def __init__(self, name, age):
print('call __init__method')
self.name = name
self.age = age
def __new__(cls, *args, **kwargs): # 创建类的对象
print('call __new__method')
print(args)
return super(Programer, cls).__new__(cls)
def __init__(self, name, age):
print('call __init__method')
self.name = name
self.age = age
2021-05-11
class Programer(object):
def __init__(self,name,age,weight):#构造函数
self.name=name
self._age=age
self.__weight=weight#私有属性
def __init__(self,name,age,weight):#构造函数
self.name=name
self._age=age
self.__weight=weight#私有属性
2021-05-11
class BackendProgramer(Programer):#子类继承父类
def __init__(self,name,age,weight,language):#构造方法
super(BackendProgramer, self).__init__(name,age,weight)#用super调用父类的构造方法
self.language=language
def __init__(self,name,age,weight,language):#构造方法
super(BackendProgramer, self).__init__(name,age,weight)#用super调用父类的构造方法
self.language=language
2021-05-11
if __name__=='__main__':
programer=BackendProgramer('Albert',25,80,'Python')
print(dir(programer))
print(programer.__dict__)
print(type(programer))
print(isinstance(programer,Programer))#判断类programer是否继承了类Programer
programer=BackendProgramer('Albert',25,80,'Python')
print(dir(programer))
print(programer.__dict__)
print(type(programer))
print(isinstance(programer,Programer))#判断类programer是否继承了类Programer
2021-05-11
if __name__=='__main__':
programer=Programer('Albert',25,80)
print(dir(programer))
print(programer.__dict__)
print(Programer.get_hobby())
print (programer.get_weight)
print (programer.self_introduction())
programer=Programer('Albert',25,80)
print(dir(programer))
print(programer.__dict__)
print(Programer.get_hobby())
print (programer.get_weight)
print (programer.self_introduction())
2021-05-11
@property#像访问属性一样访问方法
def get_weight(self):
return self.__weight
def self_introduction(self):
print('My name is %s\nI am %s years old\n'%(self.name,self._age))
def get_weight(self):
return self.__weight
def self_introduction(self):
print('My name is %s\nI am %s years old\n'%(self.name,self._age))
2021-05-11
class Programer(object):
hobby='Play Computer'
def __init__(self,name,age,weight):#构造函数
self.name=name
self._age=age
self.__weight=weight#私有属性
@classmethod#调用类方法
def get_hobby(cls):#直接调用类里面的属性
return cls.hobby
hobby='Play Computer'
def __init__(self,name,age,weight):#构造函数
self.name=name
self._age=age
self.__weight=weight#私有属性
@classmethod#调用类方法
def get_hobby(cls):#直接调用类里面的属性
return cls.hobby
2021-05-11