zoukankan      html  css  js  c++  java
  • 【17】有关python面向对象编程的提高【多继承、多态、类属性、动态添加与限制添加属性与方法、@property】

    一、多继承

    案例1:小孩继承自爸爸,妈妈。在程序入口模块再创建实例调用执行

    #father模块
    class Father(object):
        def __init__(self,money):
            self.money = money
    
        def play(self):
            print("play")
        def eat(self):
            print("eat")
    
    
    #mother模块
    class Mother(object):
        def __init__(self,faceValue):
            self.faceValue = faceValue
    
        def watchTV(self):
            print("watch TV")
        def drink(self):
            print("drink!")
    
    #child模块
    from father import Father
    from mother import Mother
    
    # 小孩继承自父亲,母亲
    class Child(Father,Mother):
        def __init__(self,money,faceValue):
            Father.__init__(self,money)
            Mother.__init__(self,faceValue)
    
    #调用,程序启动模块
    def main():
        child = Child(200,1000)
        print(child.money,child.faceValue)
        child.eat()
        child.play()
        child.watchTV()
        child.drink()
    
    
    if __name__ == '__main__':
        main()
    
    '''
    输出:
    200 1000
    eat
    play
    watch TV
    drink!
    
    '''# 注意:当两个父类的方法是相同的名字的话,默认调用写在多继承前边的那个类的方法。

    二、多态

    案例:猫和老鼠都是继承自动物类,动物类有基本的名字属性和吃的行为,再定义一个人类,采用多态的方式来喂每一个动物。

    # 1 先定义一个animal类,供 任意一个新加入的动物继承
    class Animal(object):
        def __init__(self,name):
            self.name = name
    
        def eat(self):
            print(self.name+"")
    
    # 2 创建一个猫类,继承自父类animal类 from animal import Animal # 让Cat继承自animal父类 class Cat(Animal): def __init__(self,name): super(Cat,self).__init__(name) #单继承的写法规范
    # 3 写一个老鼠类,继承父类 from animal import Animal class Mouse(Animal): def __init__(self, name): super(Mouse, self).__init__(name)

    # 4 写一个人类,其中带有喂养任何动物的方法 from cat import Cat from mouse import Mouse class Person(object): ''' # 多种动物的话就太麻烦了 def feedCat(self,cat): print("投放食物") cat.eat() def feedMouse(self,mouse): print("给你食物") mouse.eat() ''' #用下述多态思想 --- 新的方法 def feedAnimal(self,ani): print("给你食物") ani.eat()

    # 5 程序执行入口文件 from cat import Cat from mouse import Mouse from person import Person tom = Cat("Tom") jerry = Mouse("jerry") # 定义一个人类,让他可以喂猫,老鼠 per = Person() # 多态的喂的方法 per.feedAnimal(tom) per.feedAnimal(jerry)

    '''
    输出:

    给你食物
    Tom吃
    给你食物
    jerry吃

    '''

    三、类属性与对象属性

    # 类属性: 用类名来调用的属性
    # 对象属性:对象调用的属性 是__init__()的属性
    
    class Person(object):
        # 这里的属性是类属性,用类名来调用
        name = "jiajia"
        # 下边的是对象属性
        def __init__(self, name):
            self.name = name
    
    print(Person.name)
    #输出:jiajia 打印的是类属性的信息
    
    p=Person("LiHua")
    print(p.name)
    # 输出 LiHua 打印的是对象属性
    
    '''
    总结:
    0 类属性是类名来调用的,对象属性是对象调用的
    1 类属性的优先级是高于对象属性
    2 用对象的方式访问对象属性,是打印出的是类属性信息
    如:del p.name
        print(p.name)
    此时就会打印出同名的类属性。
    '''

    四、动态添加属性和方法与限制动态添加属性与方法

    # 动态的添加属性和方法
    
    # 创建一个空的类
    class Person(object):
        # 在定义类的时候,添加一个__slots__属性,来限制动态添加的属性信息
        __slots__ = ('name','age','speak')
    
    
    
    per = Person()
    #1 动态的添加属性
    per.name = '佳佳'
    
    #2 动态的添加方法
    from types import MethodType
    def say(self):
        print("我是中国人!")
    
    per.speak = MethodType(say,per)
    #调用
    per.speak()
    
    # 此处出错是因为此属性是无法动态的添加进去的,上边的slots方法里面限制的名字没有这个的。
    per.facevalue = 15   
    
    
    # 思考:如果想要限制实例的属性怎么办?如只需要添加 name age height
    # 解决方案
    # 在定义类的时候,添加一个__slots__属性,来限制动态添加的属性信息

    五、@property

    这个装饰器主要解决的是如何把私有的属性按照正常的 方法来访问(不写set get方法)

    # 如果想把__name 这种的可以直接用per.__name访问的话,不用函数来解决就需要用property解决啦

    # 如果想把__name 这种的可以直接用per.__name访问的话,不用函数来解决就需要用property解决啦
    
    class Person(object):
        def __init__(self,age,name):
            #属性是直接外部暴露的
            self.age = age
            #限制访问的方法解决,但是又需要写set和get方法才可以访问
            self.__name = name
        '''
        def setName(self,name):
            self.__name=name
        def getName(self):
            return self.__name
        '''
        # 弃用上述的方法,用下边的装饰器的方法,访问就可以直接用对象点 对应的方法来访问啦、
        @property
        def name(self):
            return self.__name
        @name.setter
        def name(self,name):
            if name =='':
                print("不可以输入为空!")
            else:
                self.__name= name
    
    per = Person(24,'张老师')
    # 因为上边用了property 所以对于私有的属性下边依旧可以用对象名+点+属性的方式来访问到。
    per.name = "孙鑫"
    print(per.name)
  • 相关阅读:
    虚拟机linux下git clone 报SSL connect error错误
    TooManyRedirects错误
    windows2008 使用 opencv_python 出现 DLL load failed错误
    禁止别人通过开发人员工具查看网站代码
    pipreqs 执行报错问题
    Vue-router 报NavigationDuplicated的解决方案
    git 记住用户密码
    获取python所有依赖包
    修改pip的安装源
    使用pycharm发布python程序到ubuntu中运行
  • 原文地址:https://www.cnblogs.com/jiyongjia/p/9866457.html
Copyright © 2011-2022 走看看