继承多态项目(代理商管理房地产)
Posted su-sir
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了继承多态项目(代理商管理房地产)相关的知识,希望对你有一定的参考价值。
房地产应用:
需求:
房产分类:房子(house)和公寓(Apartment)
房子的属性:面积、卧室个数、浴室个数、第几层、车库、院子
公寓的属性:面积、卧室个数、浴室个数、阳台、洗衣店
出售类型:出售(Purchase)和租赁(Rental)
出售的属性:销售价格、物业费
租赁的属性:租金、公共设备、是否有家具设备
Agent:管理她所拥有的所有房产
涉及到的名称翻译:
面积:square_feet
卧室个数:num_bedrooms
浴室个数:num_baths
第几层:num_stories
车库:garage
院子:fenced
阳台:balcony
洗衣店:laundry
销售价格:price
物业费:taxes
租金:rent
公共设备:utilities
家具设备:furnished
def get_valid_input(input_string, valid_options): input_string += "({})".format(",".join(valid_options)) response = input(input_string) while response.lower() not in valid_options: response = input(input_string) return response # 公共属性 class Propery: def __init__(self, square_feet="", beds="", baths="", **kwargs): # 首先把公共属性放在父类里面进行管理 super().__init__(**kwargs) self.square_feet = square_feet self.num_bedrooms = beds self.num_baths = baths def display(self): # 这是查看函数 print("房产信息") print("=================") print("面积: {}".format(self.square_feet)) print("卧室: {}".format(self.num_bedrooms)) print("浴室: {}".format(self.num_baths)) print() @staticmethod def prompt_init(): # 这是数据录入函数 return dict(square_feet=input("输入面积: "), beds=input("输入卧室数量: "), baths=input("输入浴室数量: ") ) # 公寓类 class Apartment(Propery): 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("公寓信息") print("是否有洗衣房: %s" % self.laundry) print("是否有阳台: %s" % self.balcony) parent_init = Propery.prompt_init() laundry = "" while laundry.lower() not in Apartment.valid_balconies: laundry = input("是否有洗衣设施的功能? ({})".format(",".join(Apartment.valid_laundries))) balcony = "" while balcony.lower() not in Apartment.valid_balconies: balcony = input("房子里是否有阳台? ({})".format(",".join(Apartment.valid_balconies))) parent_init.update({ "laundry": laundry, "balcony": balcony }) return parent_init @staticmethod def prompt_init(): # 编辑公寓的属性数据 parent_init = Propery.prompt_init() laundry = get_valid_input("洗衣设施的功能", Apartment.valid_laundries) balcony = get_valid_input("房子里有阳台吗?", Apartment.valid_balconies) parent_init.update({"laundry": laundry, "balcony": balcony}) return parent_init # 房子类 class House(Propery): valid_garage = ("attached", "detached", "none") valid_fenced = ("yes", "no") def __init__(self, num_stories="", garage="", fenced="", **kwargs): # 把房子独有的属性加进来 super().__init__(**kwargs) self.garage = garage self.num_stories = num_stories self.fenced = fenced def display(self): # 查看房屋所有的属性 super().display() print("房屋详细信息") print("楼层数: {}".format(self.num_stories)) print("车库: {}".format(self.garage)) print("院子: {}".format(self.fenced)) @staticmethod def prompt_init(): # 编辑房子所有的属性 parent_init = Propery.prompt_init() fenced = get_valid_input("是否有院子? ", House.valid_fenced) garage = get_valid_input("是否有车库? ", House.valid_garage) num_stories = input("有多少层? ") 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("购买详情") print("销售价格: {}".format(self.price)) print("物业费: {}".format(self.taxes)) @staticmethod def prompt_init(): # 编辑出售的属性 return dict( price=input("售价是多少? "), taxes=input("物业费是多少? ") ) class Rental: def __init__(self, furnished="", utilities="", rent="", **kwargs): super().__init__(**kwargs) self.furnished = furnished self.utilities = utilities self.rent = rent def display(self): super().display() print("租金详情") print("租金: {}".format(self.rent)) print("公共设备: {}".format(self.utilities)) print("家具设备: {}".format(self.furnished)) @staticmethod def prompt_init(): return dict(rent=input("月租多少 "), utilities=input("物业费是多少? "), furnished=get_valid_input("房子是否有家具 ", ("yes", "no")) ) class HouseRental(Rental, House): # 注意这里继承的顺序,因为display需要调用House的,所以必需把Rental前面,继承顺序是从左到右。 @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, Apartment): @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 type of property?", ("house", "apartment")).lower() payment_type = get_valid_input("what payment type?", ("purchase", "rental")).lower() PropertyClass = self.type_map[(property_type, payment_type)] init_args = PropertyClass.prompt_init() self.property_list.append(PropertyClass(**init_args)) agent = Agent() agent.add_property() agent.display_properties()
以上是关于继承多态项目(代理商管理房地产)的主要内容,如果未能解决你的问题,请参考以下文章