dependency.py 1.7 KB
Newer Older
骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
"""

对象之间的依赖关系和运算符重载

Version: 0.1
Author: 骆昊
Date: 2018-03-12

"""


class Car(object):

	def __init__(self, brand, max_speed):
		self._brand = brand
		self._max_speed = max_speed
		self._current_speed = 0

	@property
	def brand(self):
		return self._brand

	def accelerate(self, delta):
		self._current_speed += delta
		if self._current_speed > self._max_speed:
			self._current_speed = self._max_speed

	def brake(self):
		self._current_speed = 0

	def __str__(self):
		return '%s当前时速%d' % (self._brand, self._current_speed)


class Student(object):

	def __init__(self, name, age):
		self._name = name
		self._age = age

	@property
	def name(self):
		return self._name

	# 学生和车之间存在依赖关系 - 学生使用了汽车
	def drive(self, car):
		print('%s驾驶着%s欢快的行驶在去西天的路上' % (self._name, car._brand))
		car.accelerate(30)
		print(car)
		car.accelerate(50)
		print(car)
		car.accelerate(50)
		print(car)

	def study(self, course_name):
		print('%s正在学习%s.' % (self._name, course_name))

	def watch_av(self):
		if self._age < 18:
			print('%s只能观看《熊出没》.' % self._name)
		else:
			print('%s正在观看岛国爱情动作片.' % self._name)

	# 重载大于(>)运算符
	def __gt__(self, other):
		return self._age > other._age

	# 重载小于(<)运算符
	def __lt__(self, other):
		return self._age < other._age


if __name__ == '__main__':
	stu1 = Student('骆昊', 38)
	stu1.study('Python程序设计')
	stu1.watch_av()
	stu2 = Student('王大锤', 15)
	stu2.study('思想品德')
	stu2.watch_av()
	car = Car('QQ', 120)
	stu2.drive(car)
	print(stu1 > stu2)
	print(stu1 < stu2)