zoukankan      html  css  js  c++  java
  • python之7-2类的继承与多态

    • 类的继承的意思就如同父子关系一样,这个儿子继承了父亲的一切,但是在某些地方(属性)相同的时候,儿子的属性大于老子的属性(覆盖),最底层类,总会继承最接近它的那个类的属性init

    • 类的多态总是和继承相连的,没有继承,就没有多态一说.一个子类的实例,它即属于这个子类,也属于父类,比如:父亲A和儿子B,儿子B即属于儿子类,也属于人类,但是它不属于父亲类

    • 多态是面向对象语言的一个基本特性,多态意味着变量并不知道引用的对象是什么,根据引用对象的不同表现不同的行为方式。在处理多态对象时,只需要关注它的接口即可

    • 多态的实际用法是构建一个类外函数 ,函数的参数为指向类对象的变量,然后函数体返回这个变量(类对象)的方法。这样子,调用函数就可以根据实例类型,来返回对应的方法。

    • 例如下面这个例子:

    #!/usr/bin/env python
    
    # coding=utf-8
    
    #定义一个父类人类
    
    class humen(object):
    
        def __init__(self,name,eye=2,age=None):
    
            self.name = name
    
            self.eye = eye
    
            self.age = age
    
        def action(self):
    
            print "%s%u个eye,这个人已经有%u岁了" %(self.name,self.eye,self.age)
    
    
    
    class father(humen):
    
        def action(self):
    
            print "我是%s,是一名父亲" %self.name
    
    class son(father):
    
        def action(self):
    
            print "我是%s,是一名的儿子" %self.name
    
    one = humen("one",2,20)
    
    tom = father('tom')
    
    david = son('david')
    
    def actiont(hm):
    
        return hm.action()
    
    actiont(one)
    
    actiont(tom)
    
    actiont(david)
    
    
    
    aaa103439@aaa103439-pc:~/桌面/python$ python test7_类_多态.py 
    
    one有2个eye,这个人已经有20岁了
    
    我是tom,是一名父亲
    
    我是david,是一名的儿子
    
    
    • 如果想要实现在儿子实例中输出父亲实例的属性,则可以做如下修改:
    #!/usr/bin/env python
    
    # coding=utf-8
    
    #定义一个父类人类
    
    class humen(object):
    
        def __init__(self,name,eye=2,age=None):
    
            self.name = name
    
            self.eye = eye
    
            self.age = age
    
        def action(self):
    
            print "%s%u个eye,这个人已经有%u岁了" %(self.name,self.eye,self.age)
    
    
    
    class father(humen):
    
        namef = []
    
        def __init__(self,name):
    
            self.name = name
    
            father.namef = name
    
        def action(self):
    
            print "我是%s,是一名父亲" %self.name
    
    class son(humen):
    
        def action(self):
    
            print "我是%s,是%s的儿子" %(self.name,father.namef)
    
    one = humen("one",2,20)
    
    tom = father('tom')
    
    david = son('david')
    
    bob = father('bob')
    
    micheal = son('micheal')
    
    def actiont(hm):
    
        return hm.action()
    
    actiont(one)
    
    actiont(tom)
    
    actiont(david)
    
    actiont(bob)
    
    actiont(micheal)
    
    
    
    aaa103439@aaa103439-pc:~/桌面/python$ python test7_类_多态.py 
    
    one有2个eye,这个人已经有20岁了
    
    我是tom,是一名父亲
    
    我是david,是bob的儿子
    
    我是bob,是一名父亲
    
    我是micheal,是bob的儿子
    
    
    • 注释:这里将son的父类替换为了human,目的就是在son类实例调用的父类的时候,避免了father类中列表namef的最后一个元素追加进son类实例的元素
    [星空刺] |-->一颗星辰一闪即逝,支撑它的唯有方向和目的
  • 相关阅读:
    MaaS系统概述
    流处理认识
    事务补偿
    Hystrix原理与实战
    RPC概念和框架
    git remote: error: hook declined to update
    Unity CombineTexture
    Windows Powershell统计代码行数
    unity面试题二
    unity面试题一
  • 原文地址:https://www.cnblogs.com/aaa103439/p/3851315.html
Copyright © 2011-2022 走看看