016: class and objects > 多重继承与多态的例子
2016-01-23 22:04
429 查看
房屋代理模型:
1. Property
2. House
3. Apartment
4. Purchase 与 Rental
5. 多重继承
6. 代理人
7. tools
8. 主程序
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. tools
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
8. 主程序
from Agent import Agent agent = Agent() agent.add_property() agent.display_properies()
相关文章推荐
- Objective-c开发教程--MRC和ARC混编
- Cannot locate the chosen ObjectFactory implementation: spring
- Objective-C UI之自定义轮播图控件
- 在swift 项目中使用objective-c
- 关于EnumerateObjectsUsingBlock和for-in之间的较量
- Object-C--->Swift之(七)函数重载
- Iterable Object, Iterator, Generator, Generator Iterator
- Object-C--->Swift之(六)函数类型
- 编码运行两不误!KZPlayground,Objective-C中的Playgrounds
- JS 解析map 格式JSON super.makeJSONObject(req, res, map);
- Realm Swift Objective-C 学习教程
- OBJECTIVE-C中nsstring的writetofile atomically参数解释
- Object-c 代码规范
- iOS开发之Objective-c的AES加密和解密算法的实现
- Objective-C:模拟按钮点击事件理解代理模式
- Objective-C中的@property和@synthesize用法
- Caused by: java.lang.ClassNotFoundException: net.sf.json.JSONObject
- JSONObject 解析json
- Git Unlink of file '.git/objects/pack/pack-***.pack' failed. 问题解决
- Objective-C新特性__nonnull和__nullable