• python 中类的继承与方法重写


    #父类Person
    class Person():  
        def __init__(self,name,age,hair):
            self.name = name
            self.age = age
            self.hair = hair
    
        def talk(self):
            print("Person is talking----")
    
    #子类 Chinese
    class Chinese(Person):  #先继承 Person,再重构自身类
        def __init__(self,name,age,language,hair):    # name age  hair 都是继承的父类 Person
            Person.__init__(self,name,age,hair)       #继承父类的构造方法  
            self.language = language                  # Chinese 类中的 language, name、age、hair 已继承父类Person
    
        def walk(self):
            print("Person is walking----")
    
    c = Chinese('phone',21,'French','yello')
    print (c.name)
    print (c.age)
    print (c.language)
    print(c.hair)
    
    
    运行结果:
    
    phone
    21
    French
    yello
    

      

    #父类Person
    class Person():
        def __init__(self,name,age,hair):
            self.name = name
            self.age = age
            self.hair = hair
    
        def talk(self):
            print("Person is talking----")
    
    #子类 Chinese
    class Chinese(Person):  #先继承 Person,再重构自身类
        def __init__(self,name,age,language,hair):    # name age  hair 都是继承的父类 Person
            Person.__init__(self,name,age,hair)       #继承父类的构造方法
            self.language = language                  # Chinese 类中的 language, name、age、hair 已继承父类Person
    
        def walk(self):
            print("child  is walking----")
        # talk 方法,在子类当中重构该方法
        def talk(self):
            print("child is talking-----")
    
    c = Chinese('phone',21,'French','yello')
    
    print(c.talk())
    
    
    
    打印结果:
    child is talking-----
  • 相关阅读:
    html meta标签使用总结
    HTTP与HTTPS握手的那些事
    JS windows.open打开窗口并居中
    什么是响应式web设计
    phpstorm2017 激活方法
    php与mysql 绑定变量和预定义处理
    php和mysql数据库防SQL注入的有效解决办法
    如何高效率的写一个不会重复出现的随机数
    php将IP地址转换为真实地址的方法
    PHP程序员要掌握哪些技术
  • 原文地址:https://www.cnblogs.com/lixinliang/p/13810636.html
Copyright © 2020-2023  润新知