• python学习DAY10(属性方法)


    class Test(object):
        def __init__(self,name):
            self.name=name
            self.__item=None
    #--------------------------------------------------------
        @property            #属性方法
        def test(self):          #静态方法不需要传self参数
            pring("%sxxx%s"%(self.name,self.__item))
    #--------------------------------------------------------
    t=Test("lbc")
    t.test()
    #***
    TypeError:'NoneType' object is not callable  #不能调用
    #***
    修改:
    t.test   #属性方法:把一个方法变成一个静态属性,所以调用的时候不需要加括号
    
    如果方法有参数则需要在属性方法下再添加一段代码:(方法名可以不需要一致,但建议与属性方法名一致)
        @test.setter
        def test(self,item):
            print("set to item:",item)
            self.__item=item           //设立私有变量的目的是把方法中的变量存储下来,方法中的变量是指传进方法中的变量
    调用时赋值 
    t.test="lbc"
    
    #------
    属性方法无法直接通过del删除,只能通过deleter中的函数再通过del删除相应的变量,deleter同setter一样的定义方法
    #------
    
    #-------------------------示例----------------------------
    航班查询:使得用户只需得到自己的需求,而不需要了解详细的查询步骤
    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 changed to %s"%(self.flight_status,status))
        
    
    
    f = Flight("CA980")
    f.flight_status
    #f.flight_status=2
    #-------------------------------------------------------------
    #完善版
    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):
            status_dic = {
    : "canceled",
    :"arrived",
    : "departured"
            }
            print("33[31;1mHas changed the flight status to 33[0m",status_dic.get(status) )
    
        @flight_status.deleter  #删除
        def flight_status(self):
            print("status got removed...")
    
    f = Flight("CA980")
    f.flight_status
    f.flight_status =  2 #触发@flight_status.setter 
    del f.flight_status #触发@flight_status.deleter

    此代码示例参考Alex博客!

  • 相关阅读:
    mysql 约束条件 外键 forigen key 介绍
    【洛谷P4655】Building Bridges
    【CF1139D】Steps to One
    【YbtOJ#20073】钻石守卫
    【YbtOJ#20072】相似子串
    【YbtOJ#20071】礼物购买
    【洛谷P4149】Race
    【洛谷P2059】卡牌游戏
    【CF140C】New Year Snowmen
    【GMOJ4282】平方数游戏
  • 原文地址:https://www.cnblogs.com/god-for-speed/p/11362431.html
Copyright © 2020-2023  润新知