[英]Can I somehow merge these classes such that I don't have to write this again and again and my python code doesn't get redundant
import datetime
# I AM CREATING A BIKE RENTAL SYSTEM USING OOP IN PYTHON AND SOMEHOW I SEE MYSELF REPEATING SAME CONTENTS IN METHODS, CAN I SOMEHOW WRITE THIS IN BETTER FORMAT WHICH ISNT REDUNDANT? (I AM NEW TO OOP)
class BikeRental:
def __init__(self, stock=0):
#creating instances of bike rental shop
self.stock = stock
def displayStock(self):
#displaying currently available bikes to rent
print(f'We currently have {self.stock} bikes available for rent')
return self.stock
def BikeOnHourly(self, input):
#rents bike on hourly basis
if input < 0:
print('Number of bikes should be positive!')
return None
elif input > self.stock:
print(f'Sorry we have {self.stock} bikes available right now!')
return None
else:
now = datetime.datetime.now()
print(f'you have rented {input} bike/bikes on hourly basis today'
f'at {now.hour} on {now.date()}')
print('you will be charged 5$ per bike per hour.')
print('Have a great and healthy day!')
self.stock -= input
return now
def BikeOnDailyBasis(self, n):
if n< 0:
print('Number of bikes should be positive!')
return None
elif n >self.stock:
print(f'Sorry we have {self.stock} bikes available right now!')
else:
now = datetime.datetime.now()
print(f'you have rented {n} bike/bikes on hourly basis today'
f'at {now.hour} on {now.date()}')
print('you will be charged 5$ per bike per hour.')
print('Have a great and healthy day!')
self.stock -= n
return now
I'm assuming you are asking about repeating the function, not the class.我假设您要问的是重复 function,而不是 class。 If so, something like this might be helpful, with the idea being to have different functions call the one BikeOnBasis function with a varying parameter basis.如果是这样,这样的事情可能会有所帮助,其想法是让不同的函数调用具有不同参数基础的 BikeOnBasis function。 Also see polymorphism for info on doing this type of thing in more structured ways.另请参阅多态性以获取有关以更结构化的方式执行此类操作的信息。
import datetime
# I AM CREATING A BIKE RENTAL SYSTEM USING OOP IN PYTHON AND SOMEHOW I SEE MYSELF REPEATING SAME CONTENTS IN METHODS, CAN I SOMEHOW WRITE THIS IN BETTER FORMAT WHICH ISNT REDUNDANT? (I AM NEW TO OOP)
class BikeRental:
def __init__(self, stock=0):
#creating instances of bike rental shop
self.stock = stock
def displayStock(self):
#displaying currently available bikes to rent
print(f'We currently have {self.stock} bikes available for rent')
return self.stock
def BikeOnHourly(self, n):
self.BikeOnBasis(n, 'hourly')
def BikeOnDailyBasis(self, n):
self.BikeOnBasis(n, 'daily')
def BikeOnBasis(self, n, basis):
if n< 0:
print('Number of bikes should be positive!')
return None
elif n > self.stock:
print(f'Sorry we have {self.stock} bikes available right now!')
else:
now = datetime.datetime.now()
print(f'you have rented {n} bike/bikes on {basis} basis today'
f'at {now.hour} on {now.date()}')
print('you will be charged 5$ per bike per hour.')
print('Have a great and healthy day!')
self.stock -= n
return now
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.