Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射 一、类的属性

Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射
一、类的属性

1、@property属性

作用就是通过@property把一个方法变成一个静态属性

class Room:

    def __init__(self,name,length,width,height):
        self.name=name
        self.length=length
        self.width=width
        self.height=height

    @property    
    def cal_area(self):

        return self.length*self.width

r1=Room("room1",100,100,100)
print(r1.cal_area)  
Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射
一、类的属性Python全栈开发之7、面向对象编程进阶-类属性和方法、异常处理和反射
一、类的属性
class Flight(object):
    def __init__(self,name):
        self.name=name


    def checking_status(self):

        print("checking flight %s status"%self.name)

        return 1

    @property
    def flight_status(self):

        status=self.checking_status()
        if status==0:
            print("flight has canceled")
        elif status==1:
            print("flight has arrived")
        else:
            print("cannot confirm the flight status...,please check later")

    @flight_status.setter  # 修改属性
    def flight_status(self, status):
        status_dic = {
            0: "canceled",
            1: "arrived",
            2: "departured"
        }


        print("