#! usr/bin/python
# #coding=utf-8 # for chinese
#overview of classes and Methods
class data(object):
print __name__
def __init__(self,value):
self._v = value
print "this is constructor"
def create(self):
print ("creating data" , self._v)
def update(self):
print "updating data"
def delete(self):
print "deleting data"
def resetValue(self,value1):
self._v = value1
def getnewValues(self):
return self._v
datainstance =data(66)
datainstance.create()
#understanding inheritance
class Subt(data):
def update(self):
#super(self).update() python 2 not works
super(Subt,self).update() # python 2 has to done this in the parent class : data(object) . ,python 3 looks much good
print "updating data submt"
#注意:super继承只能用于新式类,用于经典类时就会报错。
#新式类:必须有继承的类,如果没什么想继承的,那就继承object
#经典类:没有父类,如果此时调用super就会出现错误:『super() argument 1 must be type, not classobj』
#implementing classes
#---------------------------- Applying Polymorphism 多型现象,多态性; 多机组合形式; 多形性;
class network:
def cable(self):print 'i am the cable '
def wifi(self): print "I am the boss the sub -class is Okay when not exist "
class tokenRing(network):
def cable(self):print 'Iam the token '
def routher(self): print 'I am the routher'
class ethernet(network):
def cable(self):print 'Iam the ethernet'
def routher(self):print 'I am the routher'
def main():
if __name__ == '__main__':
datainstance = data(88)
datainstance.create()
datainstance.resetValue(98)
print datainstance.getnewValues()
suabmt = Subt(55) # this 55 is musted for inheritance
suabmt.update()
suabmt.create() # this method also works this method from parent not children
windows =tokenRing()
mac =ethernet()
windows.cable()
mac.cable()
for obj in (windows,mac):
obj.cable()
obj.routher() #AttributeError: tokenRing instance has no attribute 'routher' so the mathod must be exist in both class
obj.wifi() #this method only exist the parent class ,well it's fine and good for sub class ,it works
main()
# understanding Methods