016: class and objects > 多重继承与多态的例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了016: class and objects > 多重继承与多态的例子相关的知识,希望对你有一定的参考价值。
房屋代理模型:
1. Property
class Property(object): def __init__(self, square_feet=‘‘, num_bedrooms=‘‘, num_baths=‘‘, **kwargs): super().__init__(**kwargs) self.square_feet = square_feet self.num_bedrooms = num_bedrooms self.num_baths = num_baths def display(self): print("PROPERTY DETAILS") print("================") print("square footage: {}".format(self.square_feet)) print("bedrooms: {}".format(self.num_bedrooms)) print("bathrooms: {}".format(self.num_baths)) print() def prompt_init(): return dict( square_feet=input("Enter the square feet:"), num_bedrooms=input("Enter number of bedrooms:"), num_baths=input("Enter number of baths:") ) prompt_init = staticmethod(prompt_init)
2. House
from Property import Property from tools import * class House(Property): valid_garage = ("attached", "detached", "none") valid_fenced = ("yes","no") def __init__(self, num_stories="", garage="", fenced="", **kwargs): super().__init__(**kwargs) self.num_stories = num_stories self.garage = garage self.fenced = fenced def display(self): super().display() print("HOUSE DETAILS") print("# of stories: {}".format(self.num_stories)) print("garage: {}".format(self.garage)) print("fenced yard: {}".format(self.fenced)) 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 prompt_init = staticmethod(prompt_init)
3. Apartment
from Property import Property from tools import * class Apartment(Property): valid_landries = ("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().dispay() print(‘APARTMENT DETAILS‘) print(‘laundry: %s‘ % self.laundry) print(‘has balcony: %s‘ % self.balcony) parent_init = Property.prompt_init() laundry = get_valid_input("What laundry facilities does the property have?", Apartment.valid_landries) balcony = get_valid_input("Does the property have a balcony?", Apartment.valid_balconies) parent_init.update({"laundry": laundry, "balcony": balcony}) return parent_init def prompt_init(): parent_init = Property.prompt_init() balcony = get_valid_input("Is the yard fenced? ", Apartment.valid_balconies) laundry = get_valid_input("Is there a garage? ", Apartment.valid_landries) parent_init.update({ "balcony": balcony, "laundry": laundry }) return parent_init prompt_init = staticmethod(prompt_init)
4. Purchase 与 Rental
class Purchase(object): def __init__(self, price=‘‘, taxes=‘‘, **kwargs): super().__init__() self.price = price self.taxes = taxes def display(self): super().display() print("PURCHASE DETAILS") print("selling price: {}".format(self.price)) print("estimated taxes: {}".format(self.taxes)) def prompt_init(): return dict( price=input("What is the selling price? "), taxes=input("What are the estimated taxes? ") ) prompt_init = staticmethod(prompt_init)
from tools import * class Rental(object): 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("RETAL DETAILS") print("rent: {}".format(self.rent)) print("estimated utilities: {}".format(self.utilities)) print("furnished: {}".format(self.furnished)) def prompt_init(): return dict( rent=input("What is the monly rent? "), utilities=input("What are the estimated utilities? "), furnished=get_valid_input("Is the propert furnished? ", ("yes", "no")) ) prompt_init = staticmethod(prompt_init)
5. 多重继承
from Purchase import Purchase from House import House class HousePurchase(Purchase, House): def prompt_init(): init = House.prompt_init() init.update(Purchase.prompt_init()) return init prompt_init = staticmethod(prompt_init)
from Rental import Rental from House import House class HouseRental(Rental, House): def prompt_init(): init = House.prompt_init() init.update(Rental.prompt_init()) return init prompt_init = staticmethod(prompt_init)
from Purchase import Purchase from Apartment import Apartment class ApartmentPurchase(Purchase, Apartment): def prompt_init(): init = Apartment.prompt_init() init.update(Purchase.prompt_init()) return init prompt_init = staticmethod(prompt_init)
from Rental import Rental from Apartment import Apartment class ApartmentRental(Rental, Apartment): def prompt_init(): init = Apartment.prompt_init() init.update(Rental.prompt_init()) return init prompt_init = staticmethod(prompt_init)
6. 代理人
from Property import Property from House import House from Apartment import Apartment from Rental import Rental from House import House from Rental import Rental from Purchase import Purchase from HouseRental import HouseRental from HousePurchase import HousePurchase from ApartmentRental import ApartmentRental from ApartmentPurchase import ApartmentPurchase from tools import * class Agent(object): type_map = { ("house", "rental"):HouseRental, ("house", "purchase"):HousePurchase, ("apartment", "rental"):ApartmentRental, ("apartment", "purchase"):ApartmentPurchase } def __init__(self): self.property_list = [] def display_properies(self): print(‘\n\n\n\n\n\n‘) print(len(self.property_list)) for prop in self.property_list: prop.display() def add_property(self): property_type = get_valid_input("what type of property? ", ("house", "apartment")).lower() payment_type = get_valid_input("what type of payment? ", ("purchase", "rental")).lower() PropertyClass = self.type_map[(property_type, payment_type)] init_args = PropertyClass.prompt_init() self.property_list.append(PropertyClass(**init_args))
7. 主程序
from Agent import Agent agent = Agent() agent.add_property() agent.display_properies()
以上是关于016: class and objects > 多重继承与多态的例子的主要内容,如果未能解决你的问题,请参考以下文章
AWS CSAA -- 04 AWS Object Storage and CDN - S3 Glacier and CloudFront
hausaufgabe--python 39 -- objects and class