Python面向对象的三大特征(封装,继承,多态)

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'>]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值