python-類的特殊成員

 

1、成員修飾符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()

 

2、特殊成員spa

class Foo:
    def __init__(self):
        print('init')
    
    def __call__(self,'args,**kwargs):
       print('call') 
    
obj = Foo()
obj()
#Foo()()

-------------------------------------------

 

3、metaclass,類的起始  code

a.python中一塊兒事物都是對象
b.
    class Foo:
        pass

    obj = Foo()
    #obj是對象,Foo類
    #Foo類也是一個對象,type的對象
相關文章
相關標籤/搜索