zoukankan      html  css  js  c++  java
  • 2、利用继承减少代码冗余

    继承解决的是类与类之间的代码冗余问题,一定是一个类是另外一个类的子类

    继承关系的查找

    总结对象之间的相似之处得到类,总结类与类之间的相似之处就得到了类们的父类

    '''
    
    
    class OldboyPeople:
        school='Oldboy'
    
    
    class OldboyStudent(OldboyPeople):
    
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
            self.score=0
    
        def choose_course(self):
            print('%s is choosing course' %self.name)
    
    
    
    class OldboyTeacher(OldboyPeople):
    
        def __init__(self,name,age,sex,level):
            self.name=name
            self.age=age
            self.sex=sex
            self.level=level
    
        def score(self,stu,num):
            stu.score=num
    '''
    
    
    

    问题:如何在子类派生出的新方法中重用父类的功能

    class OldboyPeople:
        school='Oldboy'
    
        def __init__(self, name, age, sex):
            self.name = name
            self.age = age
            self.sex = sex
    
    
    class OldboyStudent(OldboyPeople):
    
        def __init__(self,name,age,sex):
            self.name=name
            self.age=age
            self.sex=sex
            self.score=0
    
        def choose_course(self):
            print('%s is choosing course' %self.name)
    
    
    
    class OldboyTeacher(OldboyPeople):
    
        # def __init__(self,name,age,sex,level):
        #     self.name=name
        #     self.age=age
        #     self.sex=sex
        #     self.level=level
    
        def score(self,stu,num):
            stu.score=num
    
    
    stu1=OldboyStudent('李特丹',18,'female') #OldboyPeople.__init__(stu1,'李特丹',18,'female')
    print(stu1.__dict__)
    
    tea1=OldboyTeacher('egon',18,'male') ##OldboyPeople.__init__(tea1,'egon',18,'male',10)
    print(tea1.__dict__)
  • 相关阅读:
    第八场 hdu 6136 Death Podracing(dfs+思维)
    第九场 hdu 6162 Ch’s gift(树链剖分+线段树)
    树链剖分 模板
    CodeForces
    第十场 hdu 6172 Array Challenge(矩阵快速幂)
    第十场 hdu 6171 Admiral(双向bfs找交点)
    CodeForces
    第十场 hdu 6178 (bfs)
    第十场 hdu 6180 Schedule (multiset)/(思维)
    第八场 hdu 6143 Killer Names(思维题)
  • 原文地址:https://www.cnblogs.com/z-x-y/p/10046770.html
Copyright © 2011-2022 走看看