42.继承

作者: 经纶先生 | 来源:发表于2018-03-19 21:44 被阅读0次

    被继承的是父类、基类
    通过继承创建的类是 子类或者派生类

    最简单的继承

    class School_member(object):
        def __init__(self,name,age,sex):  #构造函数
            self.name = name
            self.age = age
            self.sex = sex
    
    
        def talk(self):
            info = """
            ---------%s---------
            my name is %s
            my age  is %s
            my sex  is %s         
            """%(self.name,self.name,self.age,self.sex)
            print(info)
    
    
    class Teacher(School_member):  #继承
        pass
    
    
    A = Teacher("dailiang",23,"male")
    A.talk()
    

    子类也可以有自己的方法:

    class School_member(object):
        def __init__(self,name,age,sex):  #构造函数
            self.name = name
            self.age = age
            self.sex = sex
    
        def talk(self):
            info = """
            ---------%s---------
            my name is %s
            my age  is %s
            my sex  is %s         
            """%(self.name,self.name,self.age,self.sex)
            print(info)
    
    class Teacher(School_member):  #继承
        def teching(self):
            print("%s is teaching python" %self.name)
    
    A = Teacher("dailiang",23,"male")
    A.teching()       #dailiang is teaching python
    

    子类增加参数:

    class School_member(object):
        def __init__(self,name,age,sex):  #构造函数
            self.name = name
            self.age = age
            self.sex = sex
    
    
        def talk(self):
            info = """
            ---------%s---------
            my name is %s
            my age  is %s
            my sex  is %s         
            """%(self.name,self.name,self.age,self.sex)
            print(info)
    
    
    class Student(School_member):     
        def __init__(self, name, age, sex,amount):  # #如果继承之后,要增加新的属性,需要把父类的参数也添加上
            School_member.__init__(self,name,age,sex)  #继承父类的参数
            #也可以这样写super(Student, self).__init__(name, age, sex)
            self.amount = amount
    
    
    A = Student("gangsi",73,"male",10000)
    
    print(A.amount) #10000
    print(A.talk())
    
    

    重构父类方法:

    class School_member(object):
        def __init__(self,name,age,sex):  #构造函数
            self.name = name
            self.age = age
            self.sex = sex
    
        def talk(self):
            info = """
            ---------%s---------
            my name is %s
            my age  is %s
            my sex  is %s         
            """%(self.name,self.name,self.age,self.sex)
            print(info)
    
    
    class Student(School_member):
        def __init__(self, name, age, sex,amount):
            super(Student, self).__init__(name, age, sex)
            self.amount = amount
    
        def talk(self):                               #overwriten 重构了父类的talk方法
            print("%s is teaching python,she's amount is %s"%(self.name,self.amount))
    
    A = Student("gangsi",73,"male",10000)  
    
    print(A.amount) #10000
    print(A.talk())  ##gangsi is teaching python,she's amount is 10000
    

    多继承

    class Animal(object):
        def sleep(self):
            print("---------sleep---------")
        def eat(self):
            print("-------eat-------------")
    
    class horse(Animal):
        def run(self):
            print("-------run fast-------")
    
    class lv(Animal):
        def work(self):
            print("-------work hard----")
    
    class luozi(horse,lv):   #多继承
        pass
    
    A = luozi()  
    print(A.run())   # -------run fast-------
    print(A.work())  #-------work hard----
    

    相关文章

      网友评论

          本文标题:42.继承

          本文链接:https://www.haomeiwen.com/subject/jrhmqftx.html