zoukankan      html  css  js  c++  java
  • python 面向对象的三大特征之 继承


    #继承
      #object 基类,是python定义的所有类的父类
      #经典类:不继承object的类称作经典类
      #新式类:继承object的类称作新式类
      #python 3.x统一为新式类
      #经典类是类对象,新式类是类型对象

    #经典类的继承是按照继承的顺序进行继承的

    #新式类是按照修改的优先级来继承,越后修改优先级就越高。

    class Parent:                                 #定义一个父类
      def __init__(self):
        self.age = 0
      def sing(self):
        print("sing a song 2")

    class Child(Parent):        #定义一个子类
      def sleep(self):
        print("ZZZzzzz....")
      def sing(self):                              #重写,保留父类属性的同时有自己的属性
        Parent.sing(self)
        print(self)
        print("Sing a Song 1")

    father = Parent()
    son = Child()
    print(son.age)
    son.sing() #---> Child.sing(son)

    """

    0
    sing a song 2
    <__main__.Child object at 0x00000000007ECEF0>
    Sing a Song 1

    """

  • 相关阅读:
    责任链模式(Chain of Responsibility)
    模板模式(Template Method)
    组合模式(Composite Pattern)
    原型模式(Prototype Pattern)
    策略模式(Strategy Pattern)
    状态模式(State Pattern)
    增删改查
    安卓sql
    安卓第三次作业
    安卓第四周作业
  • 原文地址:https://www.cnblogs.com/fanxuanhui-linux/p/5928897.html
Copyright © 2011-2022 走看看