1.封装
如果不希望class里面的变量被外边(class外面)调用,可以在变量前面加上__
class Mingchen :
def __init__(self,name,age):
self.a = name
self.__b = age #不希望age在外部被调用
def Fun(self):
print(self.a,self.__b)
dx = Mingchen("张三",6)
dx.Fun()
#print(dx.__b) #这行会出错
#这样可以调用
print(dir(dx)) #这一行找到 _Mingchen__b
print(dx._Mingchen__b)
#注意是外部不可以调用,里面还是可以用的
2.0.继承
格式:
class 子类名称(父类1,父类2):
注意:若()里为空白,则默认继承object
class Father(object) :
def __init__(self,name,age): #注意__init__里面的内容会在调用Father时同时执行
self.a = name
self.b = age
def Fun(self):
print(self.a,self.b)
class Son(Father) :
def __init__(self,x,y,z):
super().__init__(x,y) #调用父类里的init,此时x,y分别对应父类里的name,age
self.c = z
print("职业是"+self.c)
dx = Son("张三",26,"医生")
dx.Fun() #方法的调用
结果:
职业是医生
张三 26
说明先执行子类中的init里的内容再执行父类里init里的内容
2.1.方法重写
如果对父类里的某个属性或方法不满意可以在子类里重新书写
class Father(object) :
def __init__(self,name,age): #注意__init__里面的内容会在调用Father时同时执行
self.a = name
self.b = age
def Fun(self):
print(self.a,self.b)
class Son(Father) :
def __init__(self,x,y,z):
super().__init__(x,y) #调用父类里的init,此时x,y分别对应父类里的name,age
self.c = z
def Fun(self):
print("职业是"+self.c)
dx = Son("张三",26,"医生")
dx.Fun()
结果:
职业是医生
子类重写前后可以通过super().xxx()调用父类里被重写的方法
class Father(object) :
def __init__(self,name,age): #注意__init__里面的内容会在调用Father时同时执行
self.a = name
self.b = age
def Fun(self):
print(self.a,self.b)
class Son(Father) :
def __init__(self,x,y,z):
super().__init__(x,y) #调用父类里的init,此时x,y分别对应父类里的name,age
self.c = z
def Fun(self):
super().Fun() #这里可以调用父类里的Fun()
print("职业是"+self.c)
dx = Son("张三",26,"医生")
dx.Fun()
结果:
张三 26
职业是医生
3.0.object类
object是所有类的父类,所以任何类都有object类的属性和方法
内置函数dir()可以查看对象的所有属性
object有一个__str__()方法,用于返回一个对于“对象的描述”,对应于内置函数str()经常用于print()方法,帮我们查看对象的信息,所以经常会对__str__()进行重写
class Mingchen(object):
pass
dx = Mingchen()
print(dx) # __str__()没有重写时,输出的是内存地址
结果:
<__main__.Mingchen object at 0x000001E091507340>
class Mingchen(object) :
def __init__(self,name,age):
self.a = name
self.b = age
#重写
def __str__(self):
return "我叫{0},今年{1}岁".format(self.a,self.b)
dx = Mingchen("张三",16)
print(dx)
结果:
我叫张三,今年16岁
3.1.多态
多态(多种形态),即使不知道一个变量所引用的对象到底是什么类型,仍然可以通过这个变量调用方法,在运行过程中根据变量所引用对象的类型,动态决定调用哪个对象中的方法
class Father(object) :
def Fun1(self):
print("father")
class Son1(Father) :
def Fun1(self): #重写父类中的方法
print("son1")
class Son2(Father) :
def Fun1(self):
print("son2")
class FatherFriend(object) :
def Fun1(self):
print("father'friend")
#定义一个函数
def Fun2(a):
a.Fun1()
#调用
Fun2(Son1())
Fun2(FatherFriend())
3.2.特殊属性和特殊方法
名称 | 描述 | |
特殊属性 | __dict__ | 获得类对象或实例对象所绑定地所有属性和方法的字典 |
特殊方法 | __len__() | 通过重写__len__()方法,让内置函数len()的参数可以是自定义类型 |
__add__() | 通过重写__add__()方法,可使用自定义对象具有”+“功能 | |
__new__() | 用于创建对象 | |
__init__() | 对创建的对象进行初始化 |
1.__dict__
class Mingchen(object) :
def __init__(self,name):
self.a = name
dx = Mingchen("张三")
print(dx.__dict__) #实例对象的属性字典
print(Mingchen.__dict__) #类对象的属性字典
结果:
{'a': '张三'}
{'__module__': '__main__', '__init__': <function Mingchen.
__init__ at 0x000002560B35B0A0>, '__dict__': <attribute
'__dict__' of 'Mingchen' objects>, '__weakref__': <attribute
'__weakref__' of 'Mingchen' objects>, '__doc__': None}
2.__class__
输出所属的类
class Mingchen(object) :
def __init__(self,name):
self.a = name
dx = Mingchen("张三")
print(dx.__class__)
结果:
<class '__main__.Mingchen'>
3.__bases__
把父类找出来
class Father :
pass
class Mother :
pass
class Son(Father,Mother) :
pass
print(Son.__bases__)
结果:
(<class '__main__.Father'>, <class '__main__.Mother'>)
4.__base__
class Father :
pass
class Mother :
pass
# class Son(Father,Mother) :
class Son(Father,Mother) :
pass
print(Son.__base__)
结果:
<class '__main__.Father'>
#哪个离Son更近就打印哪个
5.__mro__
用于了解继承关系(层次结构)
class FatherFather(object):
pass
class Father(FatherFather) :
pass
class Son(Father) :
pass
print(Son.__mro__)
结果:
(<class '__main__.Son'>, <class '__main__.Father'>,
<class '__main__.FatherFather'>, <class 'object'>)
6.__subclasses__()
寻找子类
注意括号
class FatherFather(object):
pass
class Father(FatherFather) :
pass
class Son(Father) :
pass
print(FatherFather.__subclasses__())
结果:
[<class '__main__.Father'>]