Python面向对象(附练习实例)

Python面向对象(附练习实例)_第1张图片

# 创建用于计算的属性。   装饰器的使用
# 定义一个矩形类,在__init__()方法中定义两个实例属性,然后在定义一个计算矩形面积的的方法
# 并应用@property将其转换为属性,最后创建类的实例
class Rect:
    def __init__(self,width,height):
        self.width = width
        self.height = height
    @property
    def area(self):
        return self.width*self.height
rect = Rect(800,600)
print('面积为:',rect.area)
# 定义一个雁类Geese,在该类的__init__()方法中定义3个实例属性

class Geese:
    def __init__(self):
        self.neck = '脖子较长'
        self.leg = '腿部位于身体的的中心支点,行走自如'
        self.wing = '振翅频率高'
        print('我属于雁类,我有以下特征:')
        print(self.neck)
        print(self.wing)
        print(self.leg)
geese = Geese()

你可能感兴趣的:(Python基础)