1.静态方法
class Dog(object): def __init__(self,name): self.name = name @staticmethod #静态方法实际上和类没有关系,也是一个装饰器, def eat(self): # 这时候eat函数在类里面,但是类不向其传参数,这时候要调用传参数可以把实例化的类传进去 print("%s is eating %s" %(self.name,"qq")) d = Dog("ABc") d.eat(d) # 静态方法 #只是名义上归类管理,实际上静态方法访问不了任何类的任何属性和方法
2.类方法
class Dog(object): name = 'ABDc' def __init__(self,name): self.name = name @classmethod #直接调用显示没有name.类方法只能访问类变量,不能访问实例变量。特定场景,类变量和实例变量相同,某个方法只能访问类变量,就可以使用类方法 def eat(self): print("%s is eating %s" %(self.name,"qq")) d = Dog("ABc") d.eat()
3.属性方法
class Dog(object): def __init__(self,name): self.name = name self.__food = None @property # 属性方法 def eat(self): print("%s is eating %s" %(self.name,"qq")) def talk(self): print("%s talking %s" %(self.name,"oneone")) @property # 属性方法传参数 def eat1(self): print("%s is eating %s" %(self.name,self.__food)) @eat1.setter # 属性方法传参数 def eat1(self,food): print("seet to food:",food) self.__food = food # 把传入的参数存入实例化变量 @eat1.deleter def eat1(self): del self.__food print("删完了") d = Dog("ABc") d.eat # 属性方法把一个方法变成静态属性 d.talk() d.eat1 = "鸡蛋" d.eat1 # del d.eat1 # 属性方法不能直接删除 del d.eat1 # ABc is eating qq # ABc talking oneone # seet to food: 鸡蛋 # ABc is eating 鸡蛋 # 删完了
4.属性方法的用处
class Flight(object): def __init__(self,name): self.flight_name = name def checking_status(self): print("checking flight %s status " % self.flight_name) return 1 @property def flight_status(self): status = self.checking_status() if status == 0 : print("flight got canceled...") elif status == 1 : print("flight is arrived...") elif status == 2: print("flight has departured already...") else: print("cannot confirm the flight status...,please check later") @flight_status.setter def flight_status(self,status): print("%s has changing %s" %(self.flight_name,2)) f = Flight("CA980") f.flight_status f.flight_status = 2 # checking flight CA980 status # flight is arrived... # CA980 has changing 2
5.类的特殊成员方法
__doc__
class Dog(object): '''描述狗这个对象''' name = 'ABDc' def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" %(self.name,"qq")) d = Dog("ABc") print(Dog.__doc__) # 描述狗这个对象
2.__call__
class Dog(object): name = 'ABDc' def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" %(self.name,"qq")) def __call__(self, *args, **kwargs): print("runing Call",args,kwargs) class Dog(object): name = 'ABDc' def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" %(self.name,"qq")) d = Dog("ABc") d.eat() d = Dog("aDSF") d(1,2,3,name='ss')# 对象后面加(),也可以类()()
3.__dict__
class Dog(object): name = 'ABDc' def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" %(self.name,"qq")) def __call__(self, *args, **kwargs): print("runing Call",args,kwargs) print(Dog.__dict__) #打印类里面所有属性,不包括实例属性 d = Dog("aDSF") print(d.__dict__) # 打印实例属性
4.__str__
class Dog(object): name = 'ABDc' def __init__(self,name): self.name = name def eat(self): print("%s is eating %s" %(self.name,"qq")) def __call__(self, *args, **kwargs): print("runing Call",args,kwargs) def __str__(self): return "<obi:%s>"%self.name d = Dog('asdd') print(d)
#<obi:asdd>
5.
class Foo(object): def __init__(self): self.data = {} #用于索引操作,如字典,以下分别表示获取,设置,删除数据 def __getitem__(self,key): print("__getitem__",key) return self.data.get(key) def __setitem__(self, key, value): print("__setitem__",key,value) self.data[key] = value def __delitem__(self, key): print("__delitem__",key) obj = Foo() obj['name'] = 'abc' print(obj['name']) print(obj.data) del obj['name']
6.type
# class Foo(object): # def __init__(self,name): # self.name = name # # f = Foo("abc") # print(type(f)) # print(type(Foo)) # 普通方式创建类 def func(self): print("hello %s" %self.name ) def __init__(self,name,age): self.name = name self.age = age Foo =type('Foo',(object,),{'func':func, '__init__':__init__}) f = Foo('china','1000000000') f.func() print(type(Foo)) # hello world # <class 'type'>
6.__new__ 先于__init__以后看