OOP

[root@shuffle-dev py_test]$ vim sf_oop.py 
#!/usr/bin/env python                                                                                                                       
# -*- coding: utf-8 -*-

class Test(object):
    def __init__(self):
        print '生成Test'
    def prt(shuffle):
        print shuffle
        print shuffle.__class__
    def __del__(self):
        print self.__class__.__name__,'销毁'

t=Test()
t.prt()

t.name='shuffle'
print t.name
del t.name
setattr(t,'name','shuffle')
print t.name
print hasattr(t,'name')
print getattr(t,'name')

print "Test.__dict__",Test.__dict__
print " t.__dict__",t.__dict__

print Test.__doc__
print t.__doc__
print Test.__module__
print t.__module__

del t

class parent(object):
    def __init__(self):
        print '生成parent'

class subclass(Test,parent):
    def __init__(self):
        parent.__init__(self)
        super(subclass,self).__init__()
        pass
    print '---'
    Test.__init__(Test())
    print '---'                                                                                                                             
sub=subclass()

print '---Vector---'
class Vector(object):
    def __init__(self,x,y):
        self.x=x
        self.y=y
    def __str__(self):
        return 'Vector (%d, %d)' % (self.x,self.y)
    def __add__(self,other):
        return Vector(self.x+other.x,self.y+other.y)
    def __cmp__(self,other):
        return self.x**2+self.y**2-other.x**2-other.y**2

print Vector(1,2)+Vector(3,4)
print Vector(1,2)>Vector(3,4)  
[root@shuffle-dev py_test]$ ./sf_oop.py 
生成Test
<__main__.Test object at 0x2ac0ade09390>
<class '__main__.Test'>
shuffle
shuffle
True
shuffle
Test.__dict__ {'__module__': '__main__', '__del__': <function __del__ at 0x2ac0ade03c08>, 'prt': <function prt at 0x2ac0ade03b90>, '__dict__': <attribute '__dict__' of 'Test' objects>, '__weakref__': <attribute '__weakref__' of 'Test' objects>, '__doc__': None, '__init__': <function __init__ at 0x2ac0ade03b18>}
 t.__dict__ {'name': 'shuffle'}
None
None
__main__
__main__
Test 销毁
---
生成Test
生成Test
Test 销毁
---
生成parent
生成Test
---Vector---
Vector (4, 6)
False
subclass 销毁
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容