【发布时间】:2020-11-29 12:43:22
【问题描述】:
我要做一个基本的汽车租赁程序。
我有一个类Aluguer 接收以下属性:
- 开始租用
- 用户昵称
- 车辆名称
- 按小时计费
import time
now = time.time()
class Aluguer:
def __init__(self, inicio, nickname, nome, preco_base):
self.__inicio = inicio
self.__nickname = nickname
self.__nome = nome
self.__preco_base = preco_base
@property
def inicio(self):
self.__inicio = now
return self.__inicio
@property
def nickname(self):
return self.__nickname
@property
def nome(self):
return self.__nome
@property
def preco_base(self):
return self.__preco_base
要计算最初的租用时间,我可以做我刚才做的:
@property
def inicio(self):
self.__inicio = now
return self.__inicio
然后,我必须执行一个更新方法,向Aluguer 对象添加或更新(如果它们已经存在)两个新属性:已用时间和价格。
我写了以下内容:
def update(self):
tempo_fim = time.time()
elapsed_seconds = tempo_fim - self.__inicio
price = self.__preco_base
valor = (elapsed_seconds / 3600) * price
当用户归还车辆时,将执行update方法并将信息写入csv文件。在文件中注册后,该对象将从内存系统中删除。
我该怎么做?如何知道租借是否有效?
【问题讨论】:
-
欢迎来到 StackOverflow。您的一处房产是否表明正在出租?或者可能是一个组合?
-
如果在租约结束时删除对象,它存在的事实表明租约处于活动状态,不是吗?您如何跟踪您的车辆?
-
当用户租车时,会创建一个Aluguer对象,它具有以下属性(时间开始、用户昵称、车辆名称和每小时价格)。
标签: python python-3.x class