Python-100-Days/Day01-15/code/Day09/car1.py

64 lines
1.4 KiB
Python
Raw Normal View History

2018-04-27 00:00:22 +08:00
"""
属性的使用
2019-05-03 21:17:36 +08:00
- 访问器/修改器/删除器
- 使用__slots__对属性加以限制
2018-04-27 00:00:22 +08:00
Version: 0.1
Author: 骆昊
Date: 2018-03-12
"""
class Car(object):
2019-05-03 21:17:36 +08:00
__slots__ = ('_brand', '_max_speed')
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def __init__(self, brand, max_speed):
self._brand = brand
self._max_speed = max_speed
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def brand(self):
return self._brand
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@brand.setter
def brand(self, brand):
self._brand = brand
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@brand.deleter
def brand(self):
del self._brand
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@property
def max_speed(self):
return self._max_speed
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
@max_speed.setter
def max_speed(self, max_speed):
if max_speed < 0:
raise ValueError('Invalid max speed for car')
self._max_speed = max_speed
2018-04-27 00:00:22 +08:00
2019-05-03 21:17:36 +08:00
def __str__(self):
return 'Car: [品牌=%s, 最高时速=%d]' % (self._brand, self._max_speed)
2018-04-27 00:00:22 +08:00
car = Car('QQ', 120)
print(car)
# ValueError
# car.max_speed = -100
car.max_speed = 320
car.brand = "Benz"
# 使用__slots__属性限制后下面的代码将产生异常
# car.current_speed = 80
print(car)
# 如果提供了删除器可以执行下面的代码
# del car.brand
# 属性的实现
print(Car.brand)
print(Car.brand.fget)
print(Car.brand.fset)
print(Car.brand.fdel)
# 通过上面的代码帮助学生理解之前提到的包装器的概念
# Python中有很多类似的语法糖后面还会出现这样的东西