1 class Parent(object): 2 def __init__(self,name,address,assets): 3 self.__name=name 4 self._iq=90 5 self._eq=60 6 self._faceScore=40 7 self.__address=address 8 self.__assets=assets 9 10 def howManyIqEq(self): 11 print("Dad's Iq:",self._iq,"Dad's Eq:",self._eq) 12 def howManyFaceScore(self): 13 print("Dad faceScore:",self._faceScore) 14 def howManyAssets(self): 15 print("Dad Assets:",self._assets) 16 def subSomeAssets(self,quantity): 17 print("Dad Assets -",quantity) 18 self.__assets=self.__assets-quantity 19 return quantity 20 21 class Son(Parent): 22 def __init__(self,sonName,parentName,address,assets): 23 super(Son,self).__init__(parentName,address,assets) 24 self._sonFaceScore=self._faceScore+50 25 self.__name=sonName 26 self.__assets=assets 27 def howManyIqEq(self): 28 print("Son Iq:",self._iq,"Son Eq:",self._eq) 29 def howManyFaceScore(self): 30 print("Son Face Score:",self._sonFaceScore) 31 def howManyAssets(self): 32 print("Son Assets:",self.__assets) 33 def addSomeAsset(self,quantity): 34 print("Son's Assts Add+,quantity) 35 self.__assets+=quantity 36 37 oldMa=Parent("OldMa","FengXiang",5000) 38 youngMa=Son("XiaoMa","OldMa","Xi'an",0) 39 oldMa.howManyIqEq() 40 youngMa.howManyIqEq()
其中创建了父亲与儿子两个类,每个对象都有自己的属性,但儿子是可以继承父亲的部分属性,比如Iq。并可以加量继承父亲的属性,比如FaceScore。
父亲还可给儿子财产,用两个类中分别的增加与减少来实现。
这是以“上帝视角”编写的代码,并不是以父亲或儿子的视角为基础。