zoukankan      html  css  js  c++  java
  • Python中类的__init__继承

    Python中类的__init__继承


     概念:

     定义父类

    In [10]: class Person:
       ....:     def __init__(self,name,age,sex):
       ....:         self.name = name
       ....:         self.age = age
       ....:         self.sex = sex
       ....:     def get_name(self):
       ....:         print('name:',self.name)
       ....:  
    In [11]: Bob = Person('Bob',18,'男')
    
    In [12]: Bob.get_name()
    name: Bob

    【1】子类不重写__init__,实例化子类时,会自动调用父类定义的__init__

    In [13]: class Person_child(Person):
       ....:     def get_name(self):
       ....:         print('name:',self.name)
       ....:         
    
    In [14]: child = Person_child('baby',2,'')
    
    In [15]: child.get_name()
    name: baby

    【2】另一种的验证方式

    class Person:
        def __init__(self,name,age):
            self.name = 'Bob'
            self.age = 18
    
        def get_name(self):
            print('name:',self.name)
    
    class Student(Person):
        def get_name(self):
            print('name:',self.name)    
    
    Kevin = Student('Kevin',12)
    print('-------------------------')
    print(Kevin.name)    
    print(Kevin.age)    
    print('-------------------------')
    
    output
    -------------------------
    Bob
    18
    -------------------------

    【3】但重写了__init__时,实例化子类,就不会调用父类已经定义的__init__

    In [17]: class Person_child(Person):
       ....:     def __init__(self,name,age,sex):
       ....:         self.name = name
       ....:         self.age = age
       ....:         self.sex = sex
       ....:     def get_name(self):
       ....:         print('child_name:',self.name)
       ....:         
    
    In [18]: K = Person_child('Kevin',16,'')
    
    In [19]: K.get_name()
    child_name: Kevin

    【4】为了能使用或扩展父类的行为,最好显示调用父类的__init__方法

    class Person:
        def __init__(self,name,age):
            self.name = 'Bob'
            self.age = 18
    
        def get_name(self):
            print('name:',self.name)
    
    class Student(Person):
        def __init__(self,name,age,sex):
            super(Student,self).__init__(name,age)
            self.sex = sex
    
        def get_detail(self):
            print('详细信息:',self.name,'---',self.age,'---',self.sex)
    
            
    
    Kevin = Student('Kevin',12,'')
    print('-------------------------')
    print(Kevin.name)    
    print(Kevin.age)    
    print(Kevin.sex)
    print(Kevin.get_detail())    
    print('-------------------------')
    
    # output
    -------------------------
    Bob
    18
    男
    详细信息: Bob --- 18 --- 男
    None
    -------------------------

    更新....

  • 相关阅读:
    【VB/.NET】Converting VB6 to VB.NET 【Part II】【之一】
    ubuntu常用命令
    mkfifo()函数
    截图留念,“万能数据库查询分析器”作为关键字在百度和谷歌上的海量搜索结果
    vc ado 生僻使用
    [HTML5SVG]JavaScript 的新领域 动态图片处理(SVG)
    中国离“过多福利”还有多远?
    mysql 修改列为not null报错Invalid use of NULL value
    内存泄漏与内存溢出的区别
    mysql ”Invalid use of null value“ 解决方法
  • 原文地址:https://www.cnblogs.com/Echo-O/p/9398715.html
Copyright © 2011-2022 走看看