我設計了這個帶有魔術變量的OOP代碼,實際上它感覺有點複雜,所以我想糾正這個代碼最簡單的一個,而不使用Magic變量。下面的代碼是Python中的繼承。想用Python代替這些Magic Variable
而且我正在學習OOPs概念的階段,請建議我最好的OOPs實踐和哪些概念在面向對象編程人員的角度來說很重要。
class Bike():
bmodel = ''
def __init__(self,**model):
self.bmodel = model.get('bmodel')
super(Bike,self).__init__(**model)
def setmodelb(self,bmodel):
self.bmodel = bmodel
def getmodel(self):
return self.bmodel
def tostringb(self):
print("Licence",self.lno,"is Bike and Model is",self.bmodel)
class Car():
cmodel = ''
def __init__(self,**model):
self.cmodel = model.get('cmodel')
super(Car,self).__init__()
def setmodelc(self,cmodel):
self.cmodel = cmodel
def getmodel(self):
return self.cmodel
def tostringc(self):
print("Licence",self.lno,"is Car and Model is",self.cmodel)
class Vehicle(Bike,Car):
lno = ''
def __init__(self,**model):
self.lno = model.get('lno')
super(Vehicle,self).__init__(**model)
def setlno(self,lno):
self.lno = lno
def getlno(self):
return self.lno
def tostringv(self):
print("Vehicle Licence is",self.lno)
v = Vehicle()
v.setlno("CALIFORNIA99")
v.setmodelc("HONDA CITY")
v.tostringc()
v.tostringv()
輸出
Licence CALIFORNIA99 is Car and Model is HONDA CITY
Vehicle Licence is CALIFORNIA99
[Finished in 0.1s]
你的繼承是完全顛倒 - 一輛車是一輛車,一輛自行車是一輛車,但一輛車不是一輛車和自行車。也不清楚你要求什麼。 [codereview.se],也許(只爲工作代碼**註釋)? – jonrsharpe
目前還不清楚你在問什麼。我相信,如果你在網上搜索,你會發現解釋面向對象的大量資源。 – direprobs
實際情況是不使用魔術變數那就是它。 –