python-类的特殊成员
一、成员修饰符
共有成员 私有成员 class Foo: def __init__(self,name): self.name = name #self.age = age self.__age = age #私有,外部无法访问 def show(self):
return self.__age
obj = Foo('zichuan',21) print(obj.name) #obj.age print(obj.__age) #私有的
-----------------------------------------------------
class Foo:
v = '123'
def __init__(self):
pass
def show(self):
return Foo.__v
@staticmethod #静态方法
def stat(self):
return Foo.__v
#print(Foo.__v)
#ret = Foo().show()
#print(ret)
ret = Foo.stat()
print(ret)
------------------------------------------
class Foo:
def __fl(self):
return 123
def f2(self):
r = self.__f1()
return r
obj = Foo()
ret = obj.f2()
print(ret)
----------------------------------------------
class F:
def __init__(self):
self.ge = 123
seld.__game = 123
class s(F):
def __init__(self,name):
self.name = name
self.__age = 21
super(s,self).__init__()
def show(self):
print(self.name)
print(self.__age)
print(self.ge)
#print(self.__game)
s = s('zichuan')
s.show()
二、特殊成员
class Foo: def __init__(self): print('init') def __call__(self,'args,**kwargs): print('call') obj = Foo() obj() #Foo()()
-------------------------------------------
三、metaclass,类的起始
a.python中一起事物都是对象 b. class Foo: pass obj = Foo() #obj是对象,Foo类 #Foo类也是一个对象,type的对象