Доброе утро, я прохожу книгу Дасти Филлипса - Python 3 Объектно-ориентированное программирование - 2010, и есть задача заменить множественное наследование композицией. И до сих пор я не понял, как справиться с этим. Кто-то может дать мне совет или пример? Я не знаю, должен ли класс «Покупка и аренда» находиться внутри класса «Недвижимость» как метод или оставить его снаружи и изменить что-то внутри HouseRental et c.
class Property:
def __init__(self, square_feet='', beds='', baths='', **kwargs):
super().__init__(**kwargs)
self.square_feet = square_feet
self.num_beds = beds
self.num_baths = baths
def display(self):
print("PROPERTY DETAILS")
print(f'square footage: {self.square_feet}')
print(f'bedrooms: {self.num_beds}')
print(f'baths: {self.num_baths}')
print()
@staticmethod
def prompt_init():
return dict(square_feet=input("Enter the square feet: "),beds=input("Enter number of bedrooms: "),baths=input("Enter number of baths: "))
def get_valid_input(input_string, valid_option):
input_string += ' , '.join(valid_option)
response = input(input_string)
while response.lower() not in valid_option:
response = input(input_string)
return response
class Apartment(Property):
valid_laundries = ('coin', 'ensuite', 'none')
valid_balconies = ('yes','no','solarium')
def __init__(self, balcony='', laundry='', **kwargs):
super().__init__(**kwargs)
self.balcony = balcony
self.laundry = laundry
def display(self):
super().display()
print('APARTMENT DETAILS')
print(f'laundry {self.laundry}')
print(f'has balcony {self.balcony}')
@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
laundry = get_valid_input("what laundry facilities does the apartment have?", Apartment.valid_laundries)
balcony = get_valid_input('Any balcony? ',Apartment.valid_balconies)
parent_init.update ({
"laundry": laundry,
"balcony": balcony
})
return parent_init
class House(Property):
valid_garage = ('attached', 'detached', 'none')
valid_fenced = ('yes', 'no')
def __init__(self, num_stories='', garage='', fenced='', **kwargs):
super().__init__(**kwargs)
self.garage = garage
self.fenced = fenced
self.num_stories = num_stories
def display(self):
super().display()
print('House Details')
print(f'# of stories: {self.num_stories}')
print(f'garage: {self.garage}')
print(f'fenced yard: {self.fenced}')
@staticmethod
def prompt_init():
parent_init = Property.prompt_init()
fenced = get_valid_input('Is the yard fenced? ', House.valid_fenced)
garage = get_valid_input('Is there a garage? ', House.valid_garage)
num_stories = input('How many stories? ')
parent_init.update({
"fenced": fenced,
"garage": garage,
"num_stories": num_stories
})
return parent_init
class Purchase:
def __init__(self, price='', taxes='', **kwargs):
super().__init__(**kwargs)
self.price = price
self.taxes = taxes
def display(self):
super().display()
print('Purchase display')
print(f'selling price: {self.price}')
print(f'est taxes: {self.taxes}')
@staticmethod
def prompt_init():
return dict(
price=input('What is the selling price? '),
taxes=input('What are the est taxes? ')
)
class Rental:
def __init__(self, furnished='', utilities='',rent='', **kwargs):
super().__init__(**kwargs)
self.furnished = furnished
self.rent = rent
self.utilities = utilities
def display(self):
super().display()
print('Rental display')
print(f'rent price: {self.rent}')
print(f'est utilities: {self.utilities}')
print(f'furnished: {self.furnished}')
@staticmethod
def prompt_init():
return dict(
rent=input('What is the monthly rent? '),
utilities=input('What are the est utilities? '),
furnished=get_valid_input('iS the property furnished? ',('yes','no')),
)
class HouseRental(Rental, House):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Rental.prompt_init())
return init
class ApartmentRental(Rental, Apartment):
@staticmethod
def prompt_init():
init = Apartment.prompt_init()
init.update(Rental.prompt_init())
return init
class ApartmentPurchase(Purchase, Apartment):
@staticmethod
def prompt_init():
init = Apartment.prompt_init()
init.update(Purchase.prompt_init())
return init
class HousePurchase(Purchase, House):
@staticmethod
def prompt_init():
init = House.prompt_init()
init.update(Purchase.prompt_init())
return init
class Agent:
def __init__(self):
self.property_list = []
def display_properties(self):
for property in self.property_list:
property.display()
type_map = {
('house', 'rental') : HouseRental,
('house', 'purchase') : HousePurchase,
('apartment', 'rental') : ApartmentRental,
('apartment','purchase') : ApartmentPurchase
}
def add_property(self):
property_type = get_valid_input('What property? ',('house','apartment'))
payment_type = get_valid_input('What payment type? ',("purchase", "rental"))
PropertyClass = self.type_map[
(property_type, payment_type)
]
init_args = PropertyClass.prompt_init()
self.property_list.append(PropertyClass(**init_args))