1. 程式人生 > >屬性方法 靜態方法

屬性方法 靜態方法

靜態方法 屬性方法 類方法
靜態方法實際上和類沒什麼關係,但規類管 @staticmethod
類方法基本不用 類方法只能呼叫類變數,不能訪問例項變數 @classmethod
屬性方法  將一個方法變為靜態屬性  @property
-------------------------------------------
class  Dog(object):
    def  __init__(self,name):
        self.name = name
        self.__food=None

    @property
    def eat(self):
      #  print("%s is eating %s"%(self.name,self.__food))
      print("%s is eating %s" % (self.name,self.__food))
    #屬性就可以賦值
    @eat.setter
    def eat(self, food):
        print("set to food:", food)
        self.__food=food

    @eat.deleter
    def eat(self):
        del self.__food
        print("刪除屬性的方法")
d= Dog("Chengronghu")
d.eat
d.eat="baozi"
d.eat
del d.eat
#屬性方法預設刪不了
d.eat
----------------------
class Flight(object):
        def __init__(self,name):
            self.flight_name=name
        def checking_status(self):
            print("check flight  %s status"%self.flight_name)
            return  1
        @property
        def flight_status(self):
            status=self.checking_status()
            if status==0:
                print("flight  got canceing...")
            elif status==1:
                print("flight  is arriving...")
            elif status==2:
                print("flight  is departing...")
            else:
                print("flight  is unknow...")
        @flight_status.setter
        def flight_status(self,status):
            print("flight %s has changee status to %s"%(self.flight_name,status))

f=Flight("CA985")
f.flight_status
f.flight_status=2
-------------------
__doc__  提取類的描述資訊
print(__doc__)
__moudle__  獲取模組
__call__
class  Dog(object):
    def  __init__(self,name):
        self.name = name
    def __call__(self, *args, **kwargs):
        print("running",args,kwargs)

d= Dog("Chengronghu")
d(123,name="alex")
---------------------------
__dict__ 檢視類或者物件中的所有成員 D.__dict__
__str__  將記憶體地址轉為記憶體物件