Object Oriented Python

#! 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

转载于:https://www.cnblogs.com/TendToBigData/p/10501255.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值