# -*- coding: utf-8 -*- import math zero = 0.0001 class Point(object): #继承自系统的object类 '''三维点的类 ''' count = 0 #静态成员变量 def __init__(self, x=0, y=0, z=0): '''构造函数 ''' self.x = x self.y = y self.z = z Point.count += 1 def __del__(self): '''析构函数 ''' Point.count -= 1 @staticmethod def getNumber(cls): '''静态方法 ''' return Point.count @classmethod def getNumber2(cls): '''类方法 ''' return Point.count #属性 def __getattr__(self, name): try: return self.__dict__[name] except: print '没有该属性' return None def __setattr__(self, name, value): self.__dict__[name] = value def __delattr__(self, name): del self.__dict__[name] # def __getattribute__(self, name): # return self.__dict__[name] def __contains__(self, value): '''in ''' return self.x==value or self.y==value or self.z==value #转换 def __repr__(self): return str(self.x) + ' ' + str(self.y) + ' ' + str(self.z) def __int__(self): return Point(int(x), int(y), int(z)) #比较 def __eq__(self, p): '''== ''' return math.sqrt((self.x-p.x)*(self.x-p.x) + (self.y-p.y)*(self.y-p.y) + (self.z-p.z)*(self.z-p.z)) < zero def __cmp__(self, other): if x < other.x: return -1 elif x > other.x: return 1 elif y < other.y: return -1 elif y > other.y: return 1 elif z < other.z: return -1 elif z > other.z: return 1 else: return 0 def __lt__(self, other): '''< ''' if x < other.x: return True elif x > other.z: return False elif y < other.y: return True elif y > other.y: return False elif z < other.z: return True else: return False def __gt__(self, other): '''> ''' pass #运算 def __add__(self, other): return Point(self.x+other.x, self.y+other.y, self.z+other.z) def __sub__(self, other): return Point(self.x-other.x, self.y-other.y, self.z-other.z) def __mul__(self, other): if isinstance(other, Point): return self.x*other.x + self.y*other.y + self.z*other.z else: return Point(self.x*other, self.y*other, self.z*other) def __div__(self, c): return Point(self.x/c, self.y/c, self.z/c) def __mod__(self, c): '''% ''' pass def __divmod__(self, c): pass def __pow__(self, c): '''** ''' pass def __pos__(self): '''+(正号)运算符重载 ''' return Point(+self.x, +self.y, +self.z) def __neg__(self): '''-(负号)运算符重载 ''' return Point(-self.x, -self.y, -self.z) def __iadd__(self, other): '''+=运算符重载 ''' self.x += other.x self.y += other.y self.z += other.z return self def __truediv__(self, c): pass def __floordiv__(self, c): '''// ''' pass def __invert__(self): '''~取反 ''' pass def __or__(self, other): '''| ''' pass def __and__(self, other): '''& ''' pass def __xor__(self, other): '''^ ''' pass def __lshift__(self, n): '''<< ''' pass #序列 def __len__(self): return 3 def __getitem__(self, index): if index == 0: return x elif index == 1: return y elif index == 2: return z else: return None def __setitem__(self, index, value): if index == 0: x = value elif index == 1: y = value elif index == 2: z = value else: return None def __delitem__(self, index): pass def __iter__(self): '''for in ''' pass def __getslice__(self, start, end): pass print issubclass(Point, object) print Point.__bases__ p = Point(1,2,3) print isinstance(p, Point) print hasattr(p, 'z') print 4 in p
普通成员为public,单下划线开头为protected,双下划线开头为private