天天看点

魔法方法的调用

类实例可以调用所有方法,类名可以调用 类方法 和 静态方法

def __add__(self,other):
   		return

当该类的 2个 实例对象相加 时 会 自动调用add方法
           
def __str__(self):
    return

a是一个类的实例
当print(a)时会 自动调用str方法
    
           
class test():
	def __init__(self):

a = test() #创建一个对象,此时自动调用 init方法

           
class test():
	def __new__(cls): #cls表示这个类,这里是test
		return 该类的实例对象
new会在一个对象被创建前调用,即new会在init方法之前被调用,init中的self 就是new返回的实例对象
           
def __len__(self):
        return len(self._cards)
   当len(实例对象) 时会自动调用这个方法
           
def __getitem__(self, position):
        return self._cards[position]

print(deck[0])#此时调用 getitem  方法