• 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

  • 相关阅读:
    周末、广州、WEB安全测试实战训练
    WEB安全测试实战训练周末精品班课程圆满结束!
    常用渗透性测试工具
    大家还在迷信工具么?
    团购网站安全性普遍堪忧
    网页安全漏洞检测 隐藏字段
    用ModSecurity+PhantomJS进行服务器端XSS攻击检测
    关于HP WebInspect 9.1
    AQA(www.AutomationQA.com)开始连载《Web Security Testing Cookbook》学习笔记
    从团购网的漏洞看网站安全性问题
  • 原文地址:https://www.cnblogs.com/TendToBigData/p/10501255.html
Copyright © 2020-2023  润新知