单继承:
class ClassCaculator(object): def __init__(self, A, B): self.A = A self.B = B def Add(self): return self.A + self.B def Sub(self): return abs(self.A - self.B) class SubClassCaculator(ClassCaculator): def __init__(self,A,B,C): ClassCaculator.__init__(A,B) self.C = C def Mul(self): return self.A * self.B * self.C if __name__ == '__main__': CA = SubClassCaculator(2,4,6) print CA.Add() print CA.Sub() print CA.Mul()
多继承使用严格的super来继承:
class ClassCaculator(object): def __init__(self, A, B): self.A = A self.B = B def Add(self): return self.A + self.B def Sub(self): return abs(self.A - self.B) class SubClassCaculator(ClassCaculator): def __init__(self,A,B,C): super(SubClassCaculator,self).__init__(A,B) self.C = C def Mul(self): return self.A * self.B * self.C if __name__ == '__main__': CA = SubClassCaculator(2,4,6) print CA.Add() print CA.Sub() print CA.Mul()
- 微信扫码赞助
-
- 支付宝赞助
-