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>

shuffle
shuffle
True
shuffle
Test.__dict__ {'__module__': '__main__', '__del__': , 'prt': , '__dict__': , '__weakref__': , '__doc__': None, '__init__': }
 t.__dict__ {'name': 'shuffle'}
None
None
__main__
__main__
Test 销毁
---
生成Test
生成Test
Test 销毁
---
生成parent
生成Test
---Vector---
Vector (4, 6)
False
subclass 销毁

你可能感兴趣的:(OOP)